simple switch statement question

  • Thread starter Thread starter suzy
  • Start date Start date
S

suzy

in vb6 you can do this:

select case value

case 1, 2
'do something

end case


how do you do this in c#?


thanks.
 
You can do:

switch(fish)
{
case onefish:
case twofish:
case redfish:
DoSomething();
break;
case bluefish:
DoSomethingElse();
break;
}

--
Bob Powell [MVP]
Visual C#, System.Drawing

All you ever wanted to know about ListView custom drawing is in Well Formed.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*

The GDI+ FAQ: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41

*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*
 
You need a break per case.

case onefish:
break;
case twofish:
break;
case redfish:
...

Bern
 
suzy said:
i know that, but what if i want to run the same bit of code for several
values of fish?

That's what Bob's code does! When fish is onefish, twofish, or
redfish, then DoSomething is called, when it is bluefish then
DoSomethingElse is called.

Maybe this will make it click:

switch (value)
{
case 1:
case 2:
DoSomething();
break;
}

This is what you asked using VB syntax.

Or maybe if you see it written this way:

switch(fish)
{
case onefish: case twofish: case redfish:
DoSomething();
break;
case bluefish:
DoSomethingElse();
break;
}

-glenn-
 
As per Bob's example, all three fishes call the same DoSomething:

case onefish:
case twofish:
case redfish:
DoSomething();
break;

Bern
 
Per case a break else there will be a "fallthrough" syndrome where
control will fulfill all the cases until it encounters a "break
statement, ellipses or no ellipses!

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 

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