Control cannot fall through from one case label(控制不能从一个案例标签中消失)
问题描述
我正在尝试编写一个 switch 语句,该语句将根据存在的搜索文本框在搜索字段中键入搜索词.我有以下代码.但是我得到了一个控制不能从一个案例标签中落空";错误.
switch (searchType){案例SearchBooks":Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);Selenium.Click("//*[@id='SearchBooks_SearchBtn']");案例搜索作者":Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");}
<块引用>
控制不能从一个案例标签(案例SearchBooks":
)转移到另一个
控制不能从一个案例标签(案例SearchAuthors":
)转移到另一个
你错过了一些休息时间:
开关(搜索类型){案例搜索书":Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);Selenium.Click("//*[@id='SearchBooks_SearchBtn']");休息;案例搜索作者":Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");休息;}
没有它们,编译器会认为您正在尝试在 case "SearchBooks":
下的行执行后立即执行 case "SearchAuthors":
下的行,这在 C# 中是不允许的.
通过在每个 case 的末尾添加 break
语句,程序在完成后退出每个 case,无论 searchType
的值是什么.
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
You missed some breaks there:
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;
}
Without them, the compiler thinks you're trying to execute the lines below case "SearchAuthors":
immediately after the lines under case "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 of searchType
.
这篇关于控制不能从一个案例标签中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:控制不能从一个案例标签中消失


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01