Control cannot fall through from one case label

前端 未结 8 1215
旧巷少年郎
旧巷少年郎 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:05
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Case_example_1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Char ch;
                Console.WriteLine("Enter a character");
                ch =Convert.ToChar(Console.ReadLine());
                switch (ch)
                {
                    case 'a':
                    case 'e':
                    case 'i':
                    case 'o':
                    case 'u':
                    case 'A':
                    case 'E':
                    case 'I':
                    case 'O':
                    case 'U':
    
                        Console.WriteLine("Character is alphabet");
                        break;
    
                    default:
                        Console.WriteLine("Character is constant");
                        break;
    
                }
    
                Console.ReadLine();
    
            }
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 06:19

    You can do more than just fall through in C#, but you must utilize the "dreaded" goto statement. For example:

    switch (whatever)
    {
      case 2:
        Result.Write( "Subscribe" );
        break;
      case 1:
        Result.Write( "Un" );
        goto case 2;
    }
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-11-27 06:21

    Since it wassn't mentionned in the other answers, I'd like to add that if you want case SearchAuthors to be executed right after the first case is done, just like it's the case when omitting the "break" in some other programming languages where that is alowed, you can simply use "goto".

    switch (searchType)
    {
        case "SearchBooks":
        Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
        Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
        goto case "SearchAuthors";
    
        case "SearchAuthors":
        Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
        Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
        break;
    }
    
    0 讨论(0)
  • 2020-11-27 06:22

    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.

    0 讨论(0)
提交回复
热议问题