控制不能从一个案例标签中消失 [英] Control cannot fall through from one case label

查看:24
本文介绍了控制不能从一个案例标签中消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于控制不能从一个案例标签中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆