O'Reilly Forums: Confused Of Long Exercise P.385 The Deck Constructor - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Confused Of Long Exercise P.385 The Deck Constructor

#1 User is offline   Dai Wang 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 25-June 14

Posted 25 June 2014 - 11:18 AM

Class Deck {

private List<Card> cards;

public Deck(IEnuerable<Card> initialCards) {
cards = new List<Card>(initialCards);
}

public void Add(Card cardToAdd){
cards.Add(CardToAdd);
}

}

Class Form1 {

Deck deck1;
Random random = new Random();

Private void ResetDeck(int deckNumber) {
if (deckNumber == 1) {
int number of cards = random.Next(1,11);

deck1 = new Deck(new Card[] {});

for (int i = 0; i < numberOfCards; i++)
deck1.Add(new Card((Suits) random.Next(4), (Values) random.Next(1, 14)));
deck1.Sort();
}
}
}

Hi, I am newbie of C# and this is my first book of it. At page 385 of third edition , I am really confused between the connection of the the deck1 = new Deck(new Card[] {}) and cards = new List<Card>(intialCards). Is that means creating a new deck object that contains an empty array and add this empty array into List<Card> field? I am struggling to think through this logic for couple days now, but I still don't understand. Can anyone please break this transition down and explain it?

0

#2 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 758
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 26 June 2014 - 07:30 PM

Sure. Starting with the constructor for the Deck class:

public Deck(IEnumerable<Card> initialCards) {
   cards = new List<Card>(initialCards);
}


The Deck class has a a private field called card of type List<Card>, and it's initialized in the constructor. initialCards is the parameter to the constructor. When you create a new List<T> object, you can pass an IEnumerable<T> into its constructor to set its initial contents.

So the line
cards = new List<Card>(initialCards);
initializes the private cards field of the Deck class by creating a new List<Card> object, setting its contents using the initialCards constructor parameter.

Later in the code, this line:
deck1 = new Deck(new Card[] {})
calls the constructor for Deck. We just saw that the constructor has a single parameter, an IEnumerable<Card>, and it uses that parameter to initialize its private deck field. Passing
new Card[] {}

in as that parameter initializes it with an empty array of Cards, which creates an empty deck.

There are other ways you could have implemented this, I chose this way because I felt it was a good bit of code for readers to pick apart and understand. :)

I hope this helps!

Andrew
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#3 User is offline   Dai Wang 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 25-June 14

Posted 30 June 2014 - 09:46 AM

Hi, Andrew

thank you for your time to reply my post.
I think the specific part I am confusing with is that you are emptying the List<Card> cards object with an empty Card array. (Is that right? ) Is that possible and what if you could also use cards.clear() to reset the deck?

0

#4 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 758
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 01 July 2014 - 04:40 PM

No, it's not right. You're not using the empty array to clear a List. You're using it to initialize the Deck object, by passing it as a parameter to the constructor.

Have another look at the constructor for the Deck class -- that's the class you wrote earlier in the chapter that you were asked to use in the exercise. It uses an IEnumerable<Card> parameter to initialize its private cards field:

public Deck(IEnumerable<Card> initialCards)
{
        cards = new List<Card>(initialCards);
}



This is how you create a deck with a specific set of cards. It uses its cards field to keep track of which cards are in the list. That field is a List<Card> object, which gets initialized with an initial set of Card objects when the Deck is constructed. Since the Go Fish! game wants to start with an empty deck and then add to it as the cards get dealt, so it needs to initialize it with an empty collection of Card objects.
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users