Domanda

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

È stato utile?

Soluzione

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top