switch case

  • Thread starter Thread starter Alan Roberts
  • Start date Start date
A

Alan Roberts

In VB6 I could do the following

Select Case Right$(Text, 1)
Case "a","b"
do this
Case "c","d"
do this
Case else
do this
End Select


can I have multiple items per case in c#?

Thanks
Alan
 
Alan,

Yes, but you would have to do it differently:

switch (Text.Substring(Text.Length - 1, 1))
{
case "a":
case "b":
// Do something

break;

case "c":
case "d":
// Do something else.

break;

default:

// Default case.
}

Hope this helps.
 
Thanks Nicholas. A bit more cumbersome but should do the trick!

Thanks again

Alan

Nicholas Paldino said:
Alan,

Yes, but you would have to do it differently:

switch (Text.Substring(Text.Length - 1, 1))
{
case "a":
case "b":
// Do something

break;

case "c":
case "d":
// Do something else.

break;

default:

// Default case.
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Alan Roberts said:
In VB6 I could do the following

Select Case Right$(Text, 1)
Case "a","b"
do this
Case "c","d"
do this
Case else
do this
End Select


can I have multiple items per case in c#?

Thanks
Alan
 
Alan Roberts said:
Thanks Nicholas. A bit more cumbersome but should do the trick!

Slightly less cumbersome would be to use the indexer instead of
Substring - this only works for a single character, however. (Note the
single quotes instead of double quotes):

switch (Text[Text.Length-1])
{
case 'a':
case 'b':
// Do something
break;
case 'c':
case 'd':
// Do something else.
break;
default:
// Default case.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top