如何编写 switch 语句来测试 DialogResult 并提供失败逻辑
我在测试函数的返回值时得到了奇怪的结果。此代码位于 for 循环内:
DialogResult result = EvalReturnFromWS(returnMsg);
switch (result)
{
case DialogResult.Yes:
case DialogResult.No:
continue;
default:
break;
}
这是被调用函数的片段(在我的单元测试中,我总是单击“是”按钮):
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;
}
当我单击“是”时,它返回 DialogResult.Yes 但回到调用代码中,执行流向第二种情况,即“否”,这会继续,这是我不想要的。
在 StackOverflow 的其他地方,我看到一个线程建议编写一个“失败”案例,就像我为 DialogResult.Yes 所做的那样,将会失败。
简而言之,如果是,我想在 switch case 结束后恢复执行下一条语句。即“跌倒”。
编辑-抱歉造成混乱。是的,顶部片段位于 for 循环内。第二个片段位于被调用函数内部(该代码发出 MessageBox.Show)。
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 the 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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要做的是将
break;
放入DialogResult.Yes
案例中。在这种情况下,你不希望它失败。失败意味着执行从一个
case
语句转移到下一个。它与离开 switch 语句的执行无关。在您的示例中,default:
下的break;
会跳出该代码块(开关,而不是循环),并在switch< 之后继续执行/代码> 声明。同样,它不会脱离外循环。这是因为
break;
是 switch case 中的一个关键字,它会停止 switch case 块的进一步执行。但是,continue;
将继续循环,因为它不是在 switch case 中使用的东西。尝试运行此示例代码:
它只会输出
1
,因为对于i=0
和i=2
的情况,循环会继续。对于i=1
的情况,执行会转到Console.Out.WriteLine(i);
。编辑
而且,如果您想从交换机内部跳出循环,请参阅以下问题:跳出嵌套循环
What you want to do is put
break;
in theDialogResult.Yes
case. You don't want it to fall through in this case.Fall through means that execution moves from one
case
statement to the next one. It has nothing to do with execution leaving theswitch
statement. In your example, thebreak;
underdefault:
, breaks out of that block of code (the switch, not the loop) and continues execution after theswitch
statement. Again, it does NOT break out of the outer loop. This is becausebreak;
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:
It will only output
1
because for the casesi=0
andi=2
, the loop is continued. For casei=1
execution makes it toConsole.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