控制不能从一个箱子标签上落下
我正在尝试编写一个 switch 语句,该语句将根据存在的搜索文本框在搜索字段中键入搜索词。我有以下代码。但我收到“控件无法从一个案例标签掉落”错误。
switch (searchType)
{
case "SearchBooks":
Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
case "SearchAuthors":
Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
}
控件无法从一个案例标签(
案例“SearchBooks”:
)落入另一个案例标签控制不能从一个案例标签(
案例“SearchAuthors”:
)落入另一个案例标签
I am trying to write a switch statement that would type the search term in the search field depending on whichever search textbox is present. I have the following code. But I am getting a "Control cannot fall through from one case label" error.
switch (searchType)
{
case "SearchBooks":
Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
case "SearchAuthors":
Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
}
Control cannot fall through from one case label (
case "SearchBooks":
) to anotherControl cannot fall through from one case label (
case "SearchAuthors":
) to another
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您错过了一些中断:
如果没有它们,编译器会认为您尝试在
case "SearchBooks":
下的行之后立即执行case "SearchAuthors":
下面的行已执行,这在 C# 中是不允许的。通过在每个 case 末尾添加
break
语句,程序会在完成后退出每个 case,无论searchType
的值如何。You missed some breaks there:
Without them, the compiler thinks you're trying to execute the lines below
case "SearchAuthors":
immediately after the lines undercase "SearchBooks":
have been executed, which isn't allowed in C#.By adding the
break
statements at the end of each case, the program exits each case after it's done, for whichever value ofsearchType
.您需要从每个 case 标签中
break;
、throw
、goto
或return
。在循环中您也可以继续
。唯一不正确的情况是案例标签像这样堆叠时:
You need to
break;
,throw
,goto
, orreturn
from each of your case labels. In a loop you may alsocontinue
.The only time this isn't true is when the case labels are stacked like this:
在 C# 中,您可以做的不仅仅是失败,但您必须使用“可怕的”goto 语句。例如:
You can do more than just fall through in C#, but you must utilize the "dreaded" goto statement. For example:
您需要添加一个break语句:
这假设您想要处理
SearchBooks
情况或SearchAuthors
- 正如您所写的那样,在传统的 C 风格 switch 语句中,控制流将从一个 case 语句“落入”到下一个 case 语句,这意味着在searchType == "SearchBooks"
的情况下,所有 4 行代码都会被执行。您看到的编译器错误(至少部分)是为了警告程序员此潜在错误而引入的。
作为替代方案,您可以抛出错误或从方法返回。
You need to add a break statement:
This assumes that you want to either handle the
SearchBooks
case or theSearchAuthors
- as you had written in, in a traditional C-style switch statement the control flow would have "fallen through" from one case statement to the next meaning that all 4 lines of code get executed in the case wheresearchType == "SearchBooks"
.The compiler error you are seeing was introduced (at least in part) to warn the programmer of this potential error.
As an alternative you could have thrown an error or returned from a method.
在每个 switch case 的末尾,只需添加
break
语句即可解决此问题At the end of each switch case, just add the
break
-statement to resolve this problem你错过了break语句。即使在
default
情况下,也不要忘记使用break
语句。You missed break statements. Don't forget to use
break
-statements even in thedefault
case.由于其他答案中没有提到,我想补充一点,如果您希望在第一个案例之后立即执行
case SearchAuthors
,就像省略break
一样> 在其他一些允许的编程语言中,您可以简单地使用goto
。Since it wasn't mentioned in the other answers, I'd like to add that if you want
case SearchAuthors
to be executed right after the first case, just like omitting thebreak
in some other programming languages where that is allowed, you can simply usegoto
.