The reason may be that in the code
int move = 1;
private void Play_Click(object sender, EventArgs e)
{
int move = Convert.ToInt32(textBox1.Text);
timer2.Start();
}
the outer
int move
statement declares a class level scoped
move
variable
(field)
, and the
int move
statement in side the
Play_Click
method declares a
local variable
inside the method.
Whatever changes made to the move
variable inside this method are lost once the execution comes out of this method.
So the
value 1
of the
class level move
variable is only used all the time in the method
timer2_Tick_1
, hence no change is noticed.
To retain the value assigned to move replace
int move =
inside
Play_Click
method with
move =
Please see the following code to visualize the above in points
void Main()
{
VarScopeTest varScope = new VarScopeTest();
varScope.method1(5);
varScope.method2();
varScope.method1(10);
varScope.method2();
}
public class VarScopeTest {
int move =1;
int correctMove =1;
public void method1(int value){
int move =value;
correctMove = value;
Console.WriteLine ("move in side method 1={0}",move);
Console.WriteLine ("correctMove in side method 1={0}\n",correctMove);
}
public void method2(){
Console.WriteLine ("move in side method 2={0}",move);
Console.WriteLine ("correctMove in side method 2={0}\n",correctMove);
}
}