Вопрос

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