✅ 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
15 Replies
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
Ohh so you can call it from anywhere in the program
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 itselfOhh
When doing that, do i haev to remove the private and replace it with a static
nope
Or does leaving both private and static work?
private
is an accessibility modifier, static
is just an additional keyword that changes when (as opposed to where) you can access itOhh 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>()
yup
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}
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 nameOhh I see got it thank you!
Sorry to ask one last thing, but when would you use non-static and static methods then?
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
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 polymorphismAh 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!