Control cannot fall through from one case label

前端 未结 8 1257
旧巷少年郎
旧巷少年郎 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:21

    You need to break;, throw, goto, or return from each of your case labels. In a loop you may also 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;
            }
    

    The only time this isn't true is when the case labels are stacked like this:

     case "SearchBooks": // no code inbetween case labels.
     case "SearchAuthors":
        // handle both of these cases the same way.
        break;
    

提交回复
热议问题