Switch statements - multiple case items on a single line. - C#

I am wondering how you can include multiple case items on a single line in C#. It’s possible in VB and it saves a lot of time when there are several items that need to be processed by the same function.

VB Example

    Select Case TextBox1.Text[indent]case "Joe", "Mark", "John"[indent]'A Single Function
   [/indent][/indent]End Select
I cannot find a way to do this in C#. Can someone please give me an example?


I think it’s like this in C#

    case "Joe":
    case "Mark":
    case "John":

                   'A Single Function

You can stack cases I believe as in PHP. I’m a VB guy so you may want to get this verified by a C# person.

But I believe that will work for you. :slight_smile:

case “string”:

I keep getting a compiler error…

Case “Joe” cannot fall into next case, use break; instead.


hm ??? i kust posted that you need a break statement there otherwise the compiler will not kno where to break out of the case

We both posted at the same time. i was talking about the post before yours. :wink:

 But this is still not working I need multiple items on the same line... Like this.

    dim i as integer
     select case i
     	case 1 To 10, 22, 23
     	case 11 To 21
     end case

in C# I have to keep doing

   case 1: break;
   case 2: break;
   case 3: break;
   case 4: break;


That’ll teach me to try and help w/ C# :rolleyes:

but according to this page http://msdn.microsoft.com/library/en-us/csref/html/vclrfTheSwitchStatement.asp?frame=true

you can just stack cases as opposed to putting them on one line, unless I’m misunderstanding something.