문제

i have problems when adding mullti values into same case:

this is my c# code

string input = combobox1.selectedvalue.ToString(); 
switch(input)
{
case "one";
     return 1;
     break;
case "two";
     return 2;
     break;
case "three" , "four":   // error here
     return 34;
     break;
default:
     return 0;
}

need your help for

도움이 되었습니까?

해결책

Just use separate labels:

string input = combobox1.selectedvalue.ToString(); 
switch(input)
{
case "one":
     return 1;
     break;
case "two":
     return 2;
     break;
case "three": 
case "four":
     return 34;
     break;
default:
     return 0;
}

See switch:

Each switch section contains one or more case labels followed by one or more statements

다른 팁

You can you the fall though, read this for more information so it's look like this

switch(input)
{
case "one":
     return 1;
     break;
case "two":
     return 2;
     break;
case "three":
case "four": 
     return 34;
     break;
default:
     return 0;
}

Right syntax is

case "three": 
case "four":
     return 34;
     break;

instead

case "three" , "four": 
     return 34;
     break;

From switch (C# Reference)

A switch statement includes one or more switch sections. Each switch section contains one or more case labels followed by one or more statements.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top