我正在尝试编写一个 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']");
}
Control cannot fall through from one case label (
case "SearchBooks":
) to anotherControl cannot fall through from one case label (
case "SearchAuthors":
) to another
最佳答案
你错过了一些休息时间:
switch (searchType)
{
case "SearchBooks":
Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
break;
case "SearchAuthors":
Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
break;
}
如果没有它们,编译器会认为您正在尝试在 case "SearchBooks":
下的行执行后立即执行 case "SearchAuthors":
下的行,这在 C# 中是不允许的。
通过在每个 case 的末尾添加 break
语句,程序会在每个 case 完成后退出,无论 searchType
是哪个值。
关于c# - 控制不能从一个案例标签中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6696692/