Question How do I call method without it's constructor?

JazzEngineer

New member
Joined
Aug 10, 2013
Messages
4
Location
Corona, CA
Programming Experience
Beginner
Attached please find a DrawPoker console game. I can't figure out how to call the CreateDeck or Shuffle methods without calling the Deck constructor. The problem is that calling the Deck constructor generates a new Deck object. This new Deck object is NOT referred to by the rest of the code. My objective is to regenerate the Deck after each hand even though I have a running total of points earned/lost. That way I don't run out of cards unless the player runs out of points. The player is credited with 100 points at the game's beginning and the game only decrements 1 point for no hand.

Another problem I'm having with the game is that it does multiple DealHands per draw. Therefore, it begins with approx. 47 cars @ opening and runs out rapidly.

Note that this console application is written in VS 2012 for those with VS 2010.

Thank you for all your help. :)

Jazz Engineer
 

Attachments

  • DrawPoker.zip
    9.5 KB · Views: 66
Last edited by a moderator:
If you want to call an instance method then you need a reference to an instance to do it. It's that simple. You say:
This new Deck object is NOT referred to by the rest of the code.
So use the Deck that IS referenced by the rest of your code. Assign that Deck to a variable that you can access later.

That said, I'm not sure why you would have a CreateDeck method that doesn't invoke a constructor. That name implies that a new Deck object is being created. Either that method is misnamed or you're trying to do something that doesn't really make sense.
 
What kind of variable?

If you want to call an instance method then you need a reference to an instance to do it. It's that simple. You say:So use the Deck that IS referenced by the rest of your code. Assign that Deck to a variable that you can access later.

That said, I'm not sure why you would have a CreateDeck method that doesn't invoke a constructor. That name implies that a new Deck object is being created. Either that method is misnamed or you're trying to do something that doesn't really make sense.
JMcHilhinney,

Thanks for the reply. My original code was:
public static Deck myDeck = new Deck();
myDeck.CreateDeck()
myDeck.Shuffle()

This wouldn't work, possibly because of the first line. My question is: What kind of variable would I create if not a variable of type Deck?
 
You obviously already have a variable named 'myDeck' so just use it. The code you just posted has created a new Deck and assigned it to 'myDeck', then calls CreateDeck and Shuffle on it. You can simply use the last two lines of that code again elsewhere in your code.

That said, it doesn't seem like your Deck class actually represents a deck of cards. It seems like it manages multiple decks of cards internally. As such, it would be be more appropriate for it to be named DeckManager or the like. A Deck class should actually represent a deck of cards. If your CreateDeck method creates a new deck of cards but you're still using the same Deck object then that's a fault in your design.
 
I'm worried that I'm not communicating well ...

You obviously already have a variable named 'myDeck' so just use it. The code you just posted has created a new Deck and assigned it to 'myDeck', then calls CreateDeck and Shuffle on it. You can simply use the last two lines of that code again elsewhere in your code.

That said, it doesn't seem like your Deck class actually represents a deck of cards. It seems like it manages multiple decks of cards internally. As such, it would be be more appropriate for it to be named DeckManager or the like. A Deck class should actually represent a deck of cards. If your CreateDeck method creates a new deck of cards but you're still using the same Deck object then that's a fault in your design.

JMcIlhinney,

Since I received this application partially developed, I don't fully understand it. That said, the Deck seems to be instantiated with a property upon load, though this is a console application and therefore no Load event exists. That is, I don't understand how the Deck is originally instantiated, I assume it is upon start-up. Since I couldn't find any other way to re-create or replenish the Deck, I wrote the code you saw in the previous email; myDeck wasn't in the original and never worked.

Did you examine the system I uploaded? (I assume I did.) Would you like me to upload it for you, again?

Thanks,

Jazz Engineer Steve
 
System done.

JMcIlhinney,

Since I received this application partially developed, I don't fully understand it. That said, the Deck seems to be instantiated with a property upon load, though this is a console application and therefore no Load event exists. That is, I don't understand how the Deck is originally instantiated, I assume it is upon start-up. Since I couldn't find any other way to re-create or replenish the Deck, I wrote the code you saw in the previous email; myDeck wasn't in the original and never worked.

Did you examine the system I uploaded? (I assume I did.) Would you like me to upload it for you, again?

Thanks,

Jazz Engineer Steve

I was told to put a method in another class that invoked the methods in both Deck and itself. That regenerated the deck per draw but the deck still began the game with 42 cards instead of 52 because, for some reason, the DealHand method was invoked internally twice before displaying a hand for playing. I submitted it as it was.

Thanks again for your help.

Jazz Engineer Steve
 

Latest posts

Back
Top Bottom