CS0120 "An object reference is required for the non-static field, method or property ThePlayerOption.thePlayerChosenOption" how to fix?

5663 views c#
2

Here is my code, I've commented where the errors are:

using System;
using ScissorsPaperRock;

namespace ScissorsPaperRock
{
    public class ThePlayerOption
    {
        public UNIT thePlayerChosenOption;
        public static void Main()
        {
            Console.WriteLine("Select an option. 1 = Scissors, 2 = Paper, 3 = Rock");
            int Option = Convert.ToInt32(Console.ReadLine());

            if (Option == 1)
            {
                thePlayerChosenOption = UNIT.SCISSORS; // CS0120
            }
            else if (Option == 2)
            {
                thePlayerChosenOption = UNIT.PAPER; // CS0120
            }
            else if (Option == 3)
            {
                thePlayerChosenOption = UNIT.ROCK; // CS0120
            }
            else
            {
                Console.WriteLine("Please try again!");
            }
        }
    }
}

I don't understand what the error wants of me, since the following code produces no errors:

using System;

namespace ScissorsPaperRock
{
    public class TheAIOption
    {
        public UNIT theAIChosenOption;
        void Start()
        {
            System.Random rnd = new System.Random(); // Makes the random class.
            int AISelect = rnd.Next(0, 2);
            {
                if (AISelect == 0)
                    theAIChosenOption = UNIT.SCISSORS;
                else if (AISelect == 1)
                    theAIChosenOption = UNIT.PAPER;
                else
                    theAIChosenOption = UNIT.ROCK;
                // This code SHOULD select one of the three enums in Options.cs and keep the selected option on hand.
            }
        }
    }
}

What is causing this error? And more importantly, how can I fix it cleanly?

Cheers.

answered question

Monsieur is the object not already there? Gonna need you to elaborate.

I posted my code and the exact error. UNIT is an enum and is being referenced from another file. There's no errors on the 2nd piece of code I posted, only where I commented. So what's wrong?

2 Answers

8

public UNIT theAIChosenOption; and public UNIT thePlayerChosenOption; are both class members.

void Start() is a class method: it can freely access class members ("object instance methods").

public static void Main() is a STATIC class method. To access thePlayerChosenOption you must either:

a) provide an object reference (the gist of your error message)

... OR ...

b) declare the member STATIC: public static UNIT thePlayerChosenOption;

From the Microsoft documentation:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0120

An object reference is required for the nonstatic field, method, or property 'member'

In order to use a non-static field, method, or property, you must first create an object instance. For more information about static methods, see Static Classes and Static Class Members. For more information about creating instances of classes, see Instance Constructors.

Here's a good article (one of many!):

C#: Static vs Non-Static Classes and Static vs Instance Methods

posted this
10

Your UNIT thePlayerChosenOption variable is declared as a class variable but you're trying to access it inside a static context. Either mark thePlayerChosenOption as static or provide an object of type ThePlayerOption inside your static method. I'm also considering that your UNIT enum is defined somewhere else in the code.

posted this

Have an answer?

JD

Please login first before posting an answer.