Static variable that can not be assigned?

2

Good day, I have a code made by me in which I use a static variable to control certain actions in other forms, the detail is that the first time I use it it behaves as I expect, but from then on remains with the value 0 and (although I explicitly place it in 1, 2 or 3). It should be noted that the variable is of the integer type.

This is the code that gives a value depending on the button that is pressed in the program

private void Button_Click(object sender, RoutedEventArgs e)
{
    opcion = 1;
    Close();
}

As you can imagine here I am working on the same form in which it is declared.

the variable is defined as follows:

public static int opcion = 0;

When I clean and pass through the statement opcion = 1; and I put the mouse on the variable this keeps showing 0.

Am I doing something wrong? try to put the volatile modifier to see if the problem was not being able to access the variable, but it did not work.

    
asked by Bloodday 26.04.2016 в 21:20
source

1 answer

2

You could define that variable in a separate class, that is

public static class Global
{
   public static int opcion = 0;
}

then from the button you would do

private void Button_Click(object sender, RoutedEventArgs e)
{
    Global.opcion = 1;
    Close();
}

from the other windows you would use Global.opcion to access the data

This way you do not leave the variable defined in a window when the idea is to access it globally.

    
answered by 26.04.2016 / 22:02
source