c# - 控制不能从一个案例标签中消失

标签 c# switch-statement

我正在尝试编写一个 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 another

Control 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/

相关文章:

c - 程序序列的奇怪行为

c# - 列表中的 OutOfMemoryException

c# - 没有中央服务器的 P2P 握手

c# - 更优雅的创建数组的方式,其中每个元素都是索引

c# - WPF - 如何在 ViewModel 之间共享集合的单个实例?

c# - 在 C# 中搜索和删除 Observable 集合中的内容?

c# - 我可以动态地将参数添加到 IEnumerable.Any 方法的 LINQ 查询吗?

javascript - 简化 If 语句

c# - 在 C# 中使用 if/else 和 switch-case 之间有什么显着区别吗?

java - 一段关于剪刀石头布游戏的Java代码