简体   繁体   中英

How to call a method without first calling it's constructor in C#

I hope I can make this work without uploading my code. I have a Draw Poker console application in VS 2012 with about 5 classes that is object oriented. Yes, it is homework but I am an experienced developer learning C#.

My objective is to replenish the deck after every hand so that the player can draw from 52 cards and conceivably lose 100 points at 1 point per draw. This is not possible if the deck is depleted after a few draws, which is happening.

Problem: I was told to call a CreateDeck method but I don't see how to do so without calling the constructor. Yet if I do, a new Deck object is instantiated but not referenced; the original deck is referenced and depleted rapidly through each draw.

Associated problem is that multiple DealHands are executed per draw thereby multiply the pace at which the deck is depleted.

Since this system is hundreds of lines of code scattered across five classes, I don't see how to include my code. While I could, obviously include the method calls, I was told that the constructor will not work on itself. Not sure what the instructor meant.

I can email the zipped system to an interested party.

Thank you.

Create a new Deck every time you start a new game:

Deck deck = new Deck();
...
Card card = deck.NextCard();

In the constructor of Deck, have code that sets up a new 52 card shuffled deck.

public class Deck
{
    public Deck ()
    {
        // Create List/Array of shuffled cards here
    }
}

or

If you really want this as a static method add a Shuffle() method to Deck

public static void Shuffle()
{
    // Shuffle cards here
}

And call

Deck.Shuffle() 

Note that you are calling Shuffle on the class not the deck instance, as in the first example. In this case, your List/Array of cards must also be static.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM