开发者

How to code a switch statement to test DialogResult and provide fall through logic

I'm getting strange results testing the return value from a function. This code is inside a for loop:

DialogResult result = EvalReturnFromWS(returnMsg);
     switch (result)
     {
         case DialogResult.Yes:
         case DialogResult.No:
              continue;
         default:
              break;
     } 

Here is a snippet from the called function (in my unit testing I always click the "yes" button):

DialogResult result = MessageBox.Show(mbText.ToString(), caption, button, icon);
switch (result)
    {
         case DialogResult.Yes:
              return DialogResult.Yes;
         case DialogResult.No:
              return DialogResult.No;
         case DialogResult.Cancel:
              return DialogResult.Cancel;
    }

When I click "Yes" it returns DialogResult.Yes but back in the calling code, execution flows to the 2nd case which is "no" and this does a continue which is what I do NOT intend.

Elsewhere on StackOverflow, I saw a thread that suggesting coding a "fall-through" case like I have for DialogResult.Yes would serve to fall through.

In short, if YES, I want to resume execution with the next statement after t开发者_开发知识库he end of the switch case(s). That is, "fall through".

EDIT - sorry for confusion. Yes, the top snippet is inside a for loop. The 2nd snippet is inside the called function (that code issues MessageBox.Show).


What you want to do is put break; in the DialogResult.Yes case. You don't want it to fall through in this case.

 switch (result)
 {
     case DialogResult.Yes:
          break; //Leaves the switch statement and continues executing code
     case DialogResult.No:
          continue; //Moves to next iteration of loop
     default:
          break; //Leaves the switch statement and continues executing code
 }

Fall through means that execution moves from one case statement to the next one. It has nothing to do with execution leaving the switch statement. In your example, the break; under default:, breaks out of that block of code (the switch, not the loop) and continues execution after the switch statement. Again, it does NOT break out of the outer loop. This is because break; is a keyword within switch cases that stops further execution of the switch case block. However, continue; will continue the loop because it's not something used within a switch case.

Try running this example code:

for (int i = 0; i < 3; i++) {
    switch (i) {
        case 0:
        case 2:
            continue;
        default:
            break;
    }
    Console.Out.WriteLine(i);
}

It will only output 1 because for the cases i=0 and i=2, the loop is continued. For case i=1 execution makes it to Console.Out.WriteLine(i);.

Edit
And, if you want to break out of the loop from within your switch, see this question: Breaking out of a nested loop


Is this in a loop? That's the only reason I can see you using 'continue' after No. Sure enough, by stacking the case options that way Yes and No will do the same thing. If you want 'Yes' to just 'fall thru' after the switch do this

case DialogResult.Yes:
    break;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜