我正在尝试编写一个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']");
}
控件不能从一个案例标签(
case "SearchBooks":
)转移到另一个案例标签( case label )
控制不能从一个case标签(case "SearchAuthors":
)切换到另一个
发布于 2011-07-15 00:29:55
你错过了一些休息的机会:
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 "SearchAuthors":
下的行之后立即执行case "SearchBooks":
下的行,这在C#中是不允许的。
通过在每个case的末尾添加break
语句,无论searchType
的值是什么,程序都会在完成后退出每个case。
发布于 2011-07-15 00:30:15
您需要从每个案例标签执行break;
、throw
、goto
或return
。在一个循环中,你也可以使用continue
。
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": // no code inbetween case labels.
case "SearchAuthors":
// handle both of these cases the same way.
break;
发布于 2016-03-16 06:05:50
除了在C#中失败之外,你还可以做更多的事情,但是你必须使用“可怕的”goto语句。例如:
switch (whatever)
{
case 2:
Result.Write( "Subscribe" );
break;
case 1:
Result.Write( "Un" );
goto case 2;
}
https://stackoverflow.com/questions/6696692
复制相似问题