C#C
C#3y ago
krixsick

✅ Complier Error CS0120, Can't understand why I can't call this private field

I just had a quesiton about my code here: In my logic method, the moveOptions has a Compiler Error CS0120, saying that I have to create an instance of my class. But if I were to create a class sheet, and put the moveOptions under the class, the other methods should be able to use it i think, (could be wrong), is it because of the static? What does the static do because in another class file, they don't use static. I know this code shouldn't work right now, but I just can't understand why I can't use the private field in the logic method of this class


using System.Collections.Generic;

namespace Rock_Paper_Scissors
{
    internal class Program
    {
        private Dictionary<int, string> moveOptions = new Dictionary<int, string>()
            {
                { 1, "Rock"},
                { 2, "Paper"},
                { 3, "Scissors" }

            };
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Rock Paper Scissors");
            Console.WriteLine("Please choose which option you want to do: ");

            var test = new Program();
            Console.Write(test.moveOptions[1]);

            ComputerMoveGenerator();
            Logic();


        }

        static int ComputerMoveGenerator()
        {
            Random random1 = new Random();
            int randomNumber = random1.Next(1, 4);
            return randomNumber;
        }

        static void Logic(int userChoice, int computerChoice)
        {
            Console.WriteLine(moveOptions[userChoice]);

        }
    }

}
Was this page helpful?