简体   繁体   中英

Switch-Case block not executing as intended?

In the following code snippet, what I intend is on every timer tick event, it should fall in 'next' case of the switch statement. However, as I Run, it goes through Odd cases first and then even cases?

What mistake I am making?

  private void timer1_Tick(object sender, EventArgs e)
  {
        switch (SCROLL_SCREEN_NO)
        {
            case 0: 
                    SCROLL_SCREEN_NO = 1;
                    break;

            case 1: label1.Text = "Param1";
                    label2.Text = "1234";
                    SCROLL_SCREEN_NO = 2;
                    break;

            case 2: label1.Text = "Param2";
                    label2.Text = "5678";
                    SCROLL_SCREEN_NO = 3;
                    break;

            case 3: label1.Text = "Param3";
                    label2.Text = "9012";
                    SCROLL_SCREEN_NO = 0;
                    break;

            case 4: label1.Text = "Param4";
                    label2.Text = "0";
                    SCROLL_SCREEN_NO = 5;
                    break;

            case 5: label1.Text = "Param5";
                    label2.Text = "02";
                    SCROLL_SCREEN_NO = 0;
                    break;

            default: { break; }
        }
    }

your code is really mistakable, try this:

private void timer1_Tick(object sender, EventArgs e)
{
   string[] L1Keys = new string[]{"Param1", "Param2","Param3","Param4","Param5"};
   string[] L2Keys = new string[]{"1234", "5678","9012","0","02"};
   label1.Text = L1Keys[SCROLL_SCREEN_NO];
   label2.Text = L2Keys[SCROLL_SCREEN_NO];

   if(SCROLL_SCREEN_NO >=0 && SCROLL_SCREEN_NO<=4)
       SCROLL_SCREEN_NO = (SCROLL_SCREEN_NO+1) % 5;
}

Your code seems to be OK. Are you using SCROLL_SCREEN_NO outside of this timer tick event? If so then please look at the code again that used SCROLL_SCREEN_NO.

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