简体   繁体   中英

I can't use the variable in c# (Windows Form App)

Im working a project in C# Visual Studio 2019 If i try to use i get this error:

  1. Error CS0103 The name 'x' does not exist in the current context

  2. Error CS0103 The name 'y' does not exist in the current context

How can i use variables normaly?

{
        public Game()
        {
            InitializeComponent();

        }

        private void Game_Load(object sender, EventArgs e)
        {
            int x = 10;
            int y = 11;

        }

        private void Lapkérés_Click(object sender, EventArgs e)
        {
            if (x > y)
            {
                MessageBox.Show("x bigger");
            }
        }
}

Because x and y are declared in a method, so they are termed local in the Game_Load Method. that means this two-variable exists in this Method and you can use those variables the entire Method's scope and the nested code blocks inside the method, but they won't exist after the method's execution is over (in this case after the execution of Game_Load ). so they won't be accessible from anywhere else.

Otherwise, if you want the variables to be used for the entire class, you should declare them out of a method and at the class-level. like this:

class Test
{
    int x;
    int y;
    
    private void TestMethod()
    {
        x = 10;
        y = 11;
    }
}

so the variables will be available for all non-static methods declared in the class.

Because they don't exist in that context. The variables are defined, and thus exist, only in the Game_Load method.

It sounds like you want them to be class-level members instead:

class Game
{
    private int x;
    private int y;

    public Game()
    {
        InitializeComponent();
    }

    private void Game_Load(object sender, EventArgs e)
    {
        this.x = 10;
        this.y = 11;
    }

    private void Lapkérés_Click(object sender, EventArgs e)
    {
        if (this.x > this.y)
        {
            MessageBox.Show("x bigger");
        }
    }
}

In this way they will be created when the instance of the class is created and will survive within that instance. Any method in the class can access its instance's values for those variables.

Note that multiple instances of the Game class would each have their own values in those variables. Other scopes exist which may be relevant in other cases. For example, the values could be static or you may store them externally in a file or database to persist outside the running application, etc.

(For this particular, likely contrived example you don't even really need Game_Load , you can just set the values directly at the class-level when declaring them or in the Game() constructor. But I'll assume the plan here is to introduce more logic in Game_Load which otherwise wouldn't belong at the class level or in the constructor.)

For your variables to be accessible, they must be initiated. Here they are in a private function which must be called this function before making your if condition

you have made x and y local variables for the Game_Load function.

You will need to move them to instance variables by declaring them at the class level, rather than the method level.

Define the variables at class level

{
    private int x;
    private int y;
    public Game()
    {
        InitializeComponent();
    }

    private void Game_Load(object sender, EventArgs e)
    {
        x = 10;
        y = 11;
    }

    private void Lapkérés_Click(object sender, EventArgs e)
    {
        if (x > y)
        {
            MessageBox.Show("x bigger");
        }
    }

}

You need to declare your variables (x and y) as private variables in your class but outside the function. Here, you only declare x and y in the Game_Load function.

Also, don't use 'é' in your function name it won't work.

I think that the answers here answer everything, but I would like to clarify some things. The variables that you in the Game_Load method are only seen by the method. They cannot be accesed anywhere else. In order to be able to use them everywhere in your class, you have to declare these two variables in the class outside of any methods:

class SomeClass{
private int x; //can be accesed anywhere in the class
private int y; // can also be accesed anywhere in the class
    public Game()
    {
        InitializeComponent();

    }

    private void Game_Load(object sender, EventArgs e)
    {
        x = 10;
        y = 11;

    }

    private void Lapkérés_Click(object sender, EventArgs e)
    {
        if (x > y)
        {
            MessageBox.Show("x bigger");
        }
    }
}

When you declare your variables as private, they can be accesed anywhere in the class. Just make sure that you do not declare these variables in your methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM