C#Boolean外部方法将无法读取

Her*_*ong 0 c# boolean

我有一个问题,这可能是非常的,但我在过去的3天里一直坚持下去.我有一个名为ApplyRules的方法,它经常被使用,因此我无法在其中定义bool,但是当我尝试在方法之外定义它时,它不会读取它.这是我的代码:

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;
        }
Run Code Online (Sandbox Code Playgroud)

GGu*_*ati 5

该方法是静态的,而布尔值是基于实例的.使布尔静态或方法非静态(可能更改布尔值).

因为静态方法无法访问基于实例的变量,所以您的方法在技术上无法"看到"布尔值,因为布尔值与您的类的实例相关联,而不是整个类.如果该类是,例如,PuzzleSolver,则每个PuzzleSolver实例都有一个solvep1布尔值.如果只有1个PuzzleSolver,那么你应该让solvep1成为一个静态布尔值(从技术上讲,这会使你的类成为一个单例,从长远来看这可能是'坏',但这似乎是一个用于学习目的的程序而不是一个长期项目).