Search code examples
c#loopsinteger

C# changed specific value int and looping


i changed the question sample, i hope everyone understand what i meant. i'm use .NET WinformApp

i want to make label1.Text gonna be show 1->2->3 and back to 1 again like repeat or looping from 1,2,3

i try this sample :

public void test(int value)
{
  label1.Text = value.ToString();
}
public void Timer1(object sender, EventArgs e)
{
   int checkvalue = 0;
   int A = 1; int B = 2; int C = 3;
   if (checkvalue == 0) { test(A); checkvalue = 1; }
   if (checkvalue == 1) { test(B); checkvalue = 2; }
   if (checkvalue == 2) { test(C); checkvalue = 0; }
}

but label1.Text just only show 3


Solution

  • As it stands, checkvalue and the values A, B, and C are local variables. That is to say: they are created when the Timer1 method is called, and cease to exist when the Timer1 method ends. The entire Timer1 method will execute within less time than the blink of an eye, so all of your logic will execute almost at once. This means that by the end of Timer1, test has been called for A, B, and C, and checkvalue has cycled through 0, 1, 2, and back to 0.

    What you need to do is define field variables within the class instead:

    public class MyFormClass : Form // this already exists in your code, but the name might be different
    {
        private int checkvalue = 0;
        // It's not clear what A, B, and C reprsent, but if they should be able to 
        // change between calls to Timer1, you may also want these to be field
        // variables or perhaps properties
    
        public void test(int value)
        {
          label1.Text = value.ToString();
        }
    

    The next problem to address is your if statements. After the first one is evaluated and its body executed, the next one will also be executed because you've now set checkvalue to the correct value for the evaluation of the boolean expression to pass. We should therefore change it to an if/else if/else pattern so that only one call to test will occur per execution of Timer1:

    public void Timer1(object sender, EventArgs e)
    {
       int A = 1; int B = 2; int C = 3;
    
       if (checkvalue == 0)
       {
           test(A);
           checkvalue = 1;
       }
       else if (checkvalue == 1)
       {
           test(B);
           checkvalue = 2;
       }
       else
       {
           test(C);
           checkvalue = 0;
       }
    }
    

    This should achieve your desired logic of looping across A, B, and C over and over.

    Another way to do this might be to use an array and a track the position. That way you could add more items without adding more if/else if/else statements:

    public class MyFormClass : Form
    {
        private int position = 0;
        private int[] values = new int[] { 1, 2, 3 };
    
        public void test(int value)
        {
          label1.Text = value.ToString();
        }    
    
        public void Timer1(object sender, EventArgs e)
        {
            test(values[position]);
            // Increment position and reset to 0 if it goes beyond
            // the end of the array
            if (++position >= values.Length)
            {
                position = 0;
            }
        }
    }