Passing data between objects

guyzer1954

New member
Joined
Jan 10, 2006
Messages
4
Programming Experience
5-10
I'm trying to learn about using OOP. I've created a class called Cards which has two public funtions. Shuffle() and GetNextCard(). When I create 2 instances of this class, one called Dealer and the other Player1. Dealer.Shuffle() shuffles the cards ok but Player1.GetNextCard() doesn't work because only the Dealer has the cards. Is there a way for Player1 object to get a card from the Dealer's shuffled deck?
 
You should think of objects in programming the same way you think of those objects in the real world. In the real world there is only one deck of cards (OK possibly more than one deck in Casinos some use 5 decks, but there is only one pile of cards). The players are not at all like the pile of cards. It would seem that the player should be a different class. The GetNextCard method should be a function that returns some value representing the next card. You would assign that value to the players card collection, which would be different than the card class.
 
You could use shared functions, therefore each instance of you class would have the same deck and your GetNextCard function would also work.
You could also use a private array to keep track of each players cards and a Public property to add or delete cards.
 
As Paszt says, you need to be clearer about what objects your classes are representing. You would have a Card class, a Deck class and a Player class. The Deck and Player classes should each have a property that represents the cards they have. The Deck class would have the Shuffle and GetNextCard methods. To deal a card to a player would look something like this:
VB.NET:
myPlayer.Cards.Add(myDeck.GetNextCard())
 
Back
Top