-3

I'm trying to create a card game. I created two classes, player and card. a player can have a hand of cards (which is an array of card objects). a player can also play a card from their hand. This is where my problem starts when I try to get the length of the hand array, it throws an error "An object reference is required for the nonstatic field, method, or property 'Player.hand'"

So what is the best way to go about creating this? I don't want to create an instance of the object because I don't know what the cards will be until the cards are dealt to each player when the game starts. Beginner coder btw so excuse me.

    public class Player
    {
        private int id;
        private string name;
        private Card[] hand;

        public Player(int playerId, string playerName, Card[] handOfCards)
        {
            id = playerId;
            name = playerName;
            hand = handOfCards;
        }
        
        static void playCard(Card aCard)
        {
            for (int i = 0; i < hand.Length; i++) //error CS0120
            {

            }
        }

    }

    public class Card
    {
        private int id;
        private string name;
        private int value;

        public Card()
        {

        }

        public Card(int cardId, string cardName, int cardValue)
        {
            id = cardId;
            name = cardName;
            value = cardValue; 

        }
John
  • 27
  • 3
  • The problem is not that the `Card` objects in the array are not initialized. It's that the array _itself_ is not. You need to initialize the array (perhaps with a fixed length?) before you can access the `Length` property (or any other member, for that matter). Otherwise, what do you expect `hand.Length` to return? Zero? A random value? – 41686d6564 stands w. Palestine Jun 05 '22 at 01:19
  • Can you share the code which create an object of Player class and calls PlayCard method? – Chetan Jun 05 '22 at 01:21
  • @41686d6564standsw.Palestine I tried `Card[] myhand = new Card[8]` before and then calling `myhand.Length` but that still gives the same error. – John Jun 05 '22 at 01:25
  • 2
    Oh, my bad. I misread the exception. That's actually a compiler error that tells you that you can't access `hand` from a static method. Just remove the `static` keyword from the `playCard` method. – 41686d6564 stands w. Palestine Jun 05 '22 at 01:27
  • @Chetan I haven't gotten to that point yet, I'm just starting with the classes and this error shows up. – John Jun 05 '22 at 01:28
  • 3
    You may want to re-consider using an `Array` as the `hand` property. A “hand” in most games may have a variable number of cards at any one time. If you use an array, then its size will be static once created. A `List` may be a better choice. – JohnG Jun 05 '22 at 01:28
  • Does this answer your question? [CS0120: An object reference is required for the nonstatic field, method, or property 'foo'](https://stackoverflow.com/q/498400/8967612) – 41686d6564 stands w. Palestine Jun 05 '22 at 01:29
  • @41686d6564standsw.Palestine I just removed static from the `playcard` method and no errors now, thank you so much!. – John Jun 05 '22 at 01:32
  • @JohnG good to know that in advance, thanks! – John Jun 05 '22 at 01:32
  • If your question is answered then please either add and accept an answer or delete the question. Otherwise, I won't be the last person to waste their time reading the question and the comments, only to find that the issue is resolved. – user18387401 Jun 05 '22 at 03:02

1 Answers1

-1

Oh, my bad. I misread the exception. That's actually a compiler error that tells you that you can't access hand from a static method. Just remove the static keyword from the playCard method.

Thank you @41686d6564standsw.Palestine for the answer

John
  • 27
  • 3
  • Please don't add "thank you" as an answer. Instead, **[accept the answer](https://stackoverflow.com/help/accepted-answer)** that you found most helpful. - [From Review](/review/low-quality-posts/31944382) – Saeed Zhiany Jun 05 '22 at 05:38
  • @SaeedZhiany - there was no answer to accept - the thanks were to the answer provided in the comment (which can't be accepted), not to another answer (which can). In this instance the correct action would be to click "looks ok", "edit" (which is what I did), or to skip. – Wai Ha Lee Jun 05 '22 at 08:59
  • @WaiHaLee IMHO, the closest action for the previous version of this answer was the action I took. we do not read comments when we review answers, so I had no idea that the first part was a quote from comments due to the previous answer's content style. furthermore, there is already one answer (this answer) so he can accept his answer later. BTW, Thanks for the feedback. – Saeed Zhiany Jun 05 '22 at 09:27