IT

하나의 케이스 레이블에서 제어 할 수 없습니다

lottoking 2020. 6. 22. 07:29
반응형

하나의 케이스 레이블에서 제어 할 수 없습니다


어떤 검색 텍스트 상자가 있는지에 따라 검색 필드에 검색어를 입력하는 스위치 문을 작성하려고합니다. 다음 코드가 있습니다. 그러나 "한 사례 레이블에서 제어 할 수 없습니다"오류가 발생합니다. 이 문제를 해결하는 방법을 알려주십시오. 미리 감사드립니다!

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 "SearchAuthors":')에서 다른 사례 라벨로 제어가 넘어갈 수 없습니다.


당신은 거기에 몇 가지 휴식을 놓쳤다 :

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 #에서는 허용되지 않습니다.

break각 사례의 끝에 문장을 추가하면 프로그램은 완료된 후에 각 값을가집니다 searchType.


당신은 필요에 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;

C #에서 넘어 설 수있는 것 이상을 수행 할 수 있지만 "두려운"goto 문을 사용해야합니다. 예를 들면 다음과 같습니다.

switch (whatever)
{
  case 2:
    Result.Write( "Subscribe" );
    break;
  case 1:
    Result.Write( "Un" );
    goto case 2;
}


break 문을 추가해야합니다.

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.


In the end of each switch case just add the break statement to resolve this problem like this-

           switch (manu)
            {
                case manufacturers.Nokia:
                    _phanefact = new NokiaFactory();
                    break;

                case manufacturers.Samsung:
                    _phanefact = new SamsungFactory();
                    break;

            }

You missed break statements.Don't forget to enter break statement even in default case.

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;
    default :
        Console.WriteLine("Default case handling");
        break;

}

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

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();

        }
    }
}

참고URL : https://stackoverflow.com/questions/6696692/control-cannot-fall-through-from-one-case-label

반응형