Control cannot fall through from one case label

前端 未结 8 1251
旧巷少年郎
旧巷少年郎 2020-11-27 05:50

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

8条回答
  •  独厮守ぢ
    2020-11-27 06:13

    You need to add a break statement:

    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;
    }
    

    This assumes that you want to either handle the SearchBooks case or the SearchAuthors - as you had written in, in a traditional C-style switch statement the control flow would have "fallen through" from one case statement to the next meaning that all 4 lines of code get executed in the case where searchType == "SearchBooks".

    The compiler error you are seeing was introduced (at least in part) to warn the programmer of this potential error.

    As an alternative you could have thrown an error or returned from a method.

提交回复
热议问题