C
C#13mo 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]);

}
}

}
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]);

}
}

}
15 Replies
ero
ero13mo ago
you can't access instance data (non-static data) in a static method static methods are basically things you can do from wherever, whenever instance data require... an instance of the class to exist for you to access it
krixsick
krixsick13mo ago
Ohh so you can call it from anywhere in the program
ero
ero13mo ago
as long as the accessibility is right since moveOptions is not static, you would need to create an instance of the Program class to access the dictionary but you're better off making it static itself
krixsick
krixsick13mo ago
Ohh When doing that, do i haev to remove the private and replace it with a static
ero
ero13mo ago
nope
krixsick
krixsick13mo ago
Or does leaving both private and static work?
ero
ero13mo ago
private is an accessibility modifier, static is just an additional keyword that changes when (as opposed to where) you can access it
krixsick
krixsick13mo ago
Ohh so they're two separate things So in order to change it to a static field I just say like private static Dictionary<int, string> moveOptions = new Dictionary<int, string>()
ero
ero13mo ago
yup
krixsick
krixsick13mo ago
Got it, So when calling a static method Since they belong to the class Would you do the class' name . {static method} And for non static it belongs to the instance of the class meaning I do the object's name. {method}
ero
ero13mo ago
that depends when you're calling the static method from somewhere in the class itself, you don't need to prefix it every time but when you aren't, you do (though this is also not true all the time, but don't worry about it right now) for example, Console is a static class with static methods so when you do Console.WriteLine, you are prefixing the WriteLine method with the Console class name
krixsick
krixsick13mo ago
Ohh I see got it thank you! Sorry to ask one last thing, but when would you use non-static and static methods then?
ero
ero13mo ago
things that don't just exist constantly say, an enemy in a game, or even the player you would spawn (instantiate) an enemy, interact with it, perhaps defeat it, and then remove and forget about it again
ZacharyPatten
ZacharyPatten13mo ago
whether or not to make something static or instanced (non-static) can be a subjective topic. in general though, - if you need to access instanced (non-static) members of your class then the method you are writing should also be instanced (non-static) - if you do not, then make the method you are writing static as a beginner, you should probably try to focus a little more on writing instanced (non-static) than static it is common for beginners to try to make absolutely everything static, which will lead to poor design descisions that being said... when I make little console games like this, I tend to use a lot of static because these console games typically don't require a lot of polymorphism
krixsick
krixsick13mo ago
Ah I see, got it thank you both for your feedback and advice I think it's just best for me to practice a little more Tysm!
Want results from more Discord servers?
Add your server
More Posts