Yummy275
Yummy275

Reputation: 313

Deck.Print is only showing 2 of spades have been added to deck list

So as the title of this question says, my Deck.Print() is only showing that 2 of spades have been added.

My theory is that for some reason the cards that are being created in Deck() arent changing the suit and face of the card so they stick to the default values of the enums (I assume the default is whatever is 0 in the enum).

From the way I look at it it should be creating the card, casting the Enum type onto I or F, and then adding that card to deck.list. Why is this not working? Thanks.

class Deck
{
    public List<Card> cards = new List<Card>();

    public Deck() // Generates all cards in the deck
    {
        for (int i = 0; i < 4; i++)
        {
            for (int f = 0; f < 13; f++)
            {
                Card card = new Card();
                card.Suit = (Suit)i;
                card.Face = (Face)f;
                cards.Add(card);
            }
        }
    }

    public void Print() // prints all cards in the deck , used for testing
    {
        foreach (var card in cards)
        {
            card.Print();
        }
    }
}
enum Suit
{
    Spades,
    Hearts,
    Diamonds,
    Clovers
}

enum Face
{
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
    Jack,
    Queen,
    King,
    Ace
}

class Card
{
    private Suit suit;
    private Face face;

    public Suit Suit { get; set; }
    public Face Face { get; set; }

    public void Print()
    {
        Console.WriteLine("{0} of {1}", face, suit);
    }
}

Upvotes: 0

Views: 47

Answers (1)

TheGeneral
TheGeneral

Reputation: 81563

Your problen is thus, you are reading what look originally/suspiciously like Backing Fields in your Print methods, which inturn has never been set.

If you dont need these fields, just use Auto Properties like you have, and remove them to save confusion

public Suit Suit { get; set; }

Modified

class Card
{
    // jsut delete these all together
    //private Suit suit; // you are printing this out and never changing it
    //private Face face; // you are printing this out and never changing it

    public Suit Suit { get; set; }
    public Face Face { get; set; }

    public void Print()
    {
      //  Console.WriteLine("{0} of {1}", face, suit);
      // print your actual properties not the backing fields that have never been set
      Console.WriteLine("{0} of {1}", Face, Suit);
    }
}

Upvotes: 3

Related Questions