Reputation: 17
I have a question, which might be very nooby, but I have been stuck on it for the last 3 days. I have a method called ApplyRules, which is used very frequently so I can't define a bool inside of it, but when I try to define it outside of the method, it doesn't read it. Here is my code:
public bool solvedp1 = false;
public static void ApplyRules()
{
if (Level.Rooms[0, 0].GetItem("Red Gem") != null
& Level.Rooms[1, 0].GetItem("Blue Gem") != null
& solvedp1 == false)
{
Console.Clear();
Console.WriteLine("You put the gem in its correct place. As soon as the gem is in position, you feel a shiver and a warm feeling enters your toes and passes through your whole body. The strange feeling in the room is gone. You hear a lock unlocking and a door shrieking as it opens..");
Console.WriteLine("Press enter to continue");
Console.ReadKey();
solvedp1 = true;
}
Upvotes: 0
Views: 784
Reputation: 5202
Static methods cannot access instance variables. However, the opposite is true, instance methods can access static variables. You can see it as: instance variables are only created when you explicitly declare in instance of that class, but, static variables are created when you reference it for the first time.
Upvotes: 1
Reputation: 1500015
Your variable is an instance variable - i.e. each instance of your class will have a separate variable.
Your method is a static method - it's not associated with any particular instance, so there's no variable available.
It's not clear why you think you can't declare a variable in a method just because it's "used very frequently" though. You should decide what kind of variable makes sense, and act accordingly:
A local variable is appropriate when the natural lifetime of the variable is the lifetime of the method, i.e. when the method has completed, the variable is irrelevant. It doesn't need to persist between method calls etc.
An instance variable is appropriate when it's part of the natural state of your class - e.g. a person's address might be a natural instance variable for a Person
class.
A static variable is appropriate when it's associated with the type itself rather than any particular instance of the type. Static variables (other than constants) should very often be avoided, on the grounds that it's sort of "global" state which is hard to reason about - it makes testing hard too.
Upvotes: 1
Reputation: 36733
Try this:
//Make this static so it is accessible to your ApplyRules() method.
public static bool solvedp1 = false;
public static void ApplyRules()
{
if (Level.Rooms[0, 0].GetItem("Red Gem") != null
& Level.Rooms[1, 0].GetItem("Blue Gem") != null
& solvedp1 == false)
{
Console.Clear();
Console.WriteLine("You put the gem in its correct place. As soon as the gem is in position, you feel a shiver and a warm feeling enters your toes and passes through your whole body. The strange feeling in the room is gone. You hear a lock unlocking and a door shrieking as it opens..");
Console.WriteLine("Press enter to continue");
Console.ReadKey();
solvedp1 = true;
}
}
Upvotes: 1
Reputation: 1025
The method is static, while the boolean is instance-based. Make either the boolean static or the method non-static (probably change the boolean).
Because static methods can't access instance-based variables, your method cannot technically "see" the boolean because the boolean is associated with instances of your class rather than your class as a whole. If the class is, say, PuzzleSolver, there is a solvedp1 boolean for each and every PuzzleSolver instance. If there is only 1 PuzzleSolver, ever, then you should make the solvedp1 a static boolean (technically, this makes your class a Singleton, which may be 'bad' for the long run, but this seems like a program for learning purposes rather than a long-term project).
Upvotes: 5