Switch statment question

  • Thread starter Thread starter Andrea Williams
  • Start date Start date
A

Andrea Williams

I used to program with VBS and I'm used to allowing multiple cases in one
CASE line. Anyway to do that C#?

VB Example:

Select Case (lngStatus)
Case 1,2,3
'do something
Case 4
'do something
End Select

Andrea
 
Here is an example

string time = "pm";
string greeting = "Good ";
switch(time){
case "pm":
greeting+="evening";
break;
case "am":
greating+="morning";
break;
default:
greating="Hello";
break;
}
MsgBox(greeting);

Cheers,

Sam
 
Maybe I wasn't clear... How would you say case "pm" and case "am"? As an
alternate to the GOTO statement. I'd like to add multiple cases to one line
if possible. Hence the "Case 1,2,3" below. if the GOTO statement is the
only way, then I'll stick to it, but just thought I'd ask

Andrea
 
Maybe I wasn't clear... How would you say case "pm" and case "am"?

Place the cases immediately one after the other.

switch(time)
{
case "am":
case "pm":
greeting += "evening";
break;

default:
greeting += "error";
break;
}

alternate to the GOTO statement. I'd like to add multiple cases to one line
if possible

You can do that also, since line breaks aren't significant, but IMHO
it's much less readable in C#.

switch(time)
{
case "am": case "pm":
greeting += "evening";
break;
...
 
kewl, thx! that worked!

Andrea


David said:
Place the cases immediately one after the other.

switch(time)
{
case "am":
case "pm":
greeting += "evening";
break;

default:
greeting += "error";
break;
}



You can do that also, since line breaks aren't significant, but IMHO
it's much less readable in C#.

switch(time)
{
case "am": case "pm":
greeting += "evening";
break;
...
 
Back
Top