switch-statement ???

C

Chris

Hi,

can you specify a range in a switch - statement ?

switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
// do something else
break;
}

Previous syntax is not allowed, I know,
but can you solve this using 'switch' ? without having to provide a case
statement for every possible value and without using 'if' ?

thanks
Christian
 
M

Michael C

Hi Chris,

We were discussing the heck out of that a few months ago, and the answer is
(in C#), no. On the other hand, if you only have two ranges as in the
example you posted, an if...else construct would be almost as efficient. If
you have only a couple of options per case statement, case can drop-through
like this:

switch (i)
{
case 100:
case 101:
// do something
break;
case 1000:
case 1001:
case 1002:
// do something else
break;
}

Thanks,
Michael C., MCDBA
 
P

Paul E Collins

Chris said:
switch (i)
{
case 100 - 999 :

That will actually compile, but it won't do what you want. (The minus
sign is subtraction, so it's equivalent to "case -899".)

You can't specify ranges in a C# switch statement.

P.
 
J

JuLiE Dxer

I thought fall-throughs in switch statements were not supported in C#
(design choice).

At least, that's what mentioned on page 224 of Microsoft's "Inside C#"
by Tom Archer
 
E

Eran Kampf

From MSDN:

Although fall through from one case label to another is not supported, it is
allowed to stack case labels, for example:
case 0:
case 1:
// do something;

Eran Kampf
http://www.ekampf.com
 
R

Richard Blewett [DevelopMentor]

Also you are aloud to transfer control from from case block to another (which gives the same functionality as fall-through, but in an obviously inteded way rather than happning by accident.

switch(i)
{
case 0:
case 1:
// do some stuff
goto case 2;
case 2:
// do some other stuff
break;
}

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

From MSDN:

Although fall through from one case label to another is not supported, it is
allowed to stack case labels, for example:
case 0:
case 1:
// do something;

Eran Kampf
http://www.ekampf.com

JuLiE Dxer said:
I thought fall-throughs in switch statements were not supported in C#
(design choice).

At least, that's what mentioned on page 224 of Microsoft's "Inside C#"
by Tom Archer



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.languages.csharp]
 
M

Michael C

"Fall-through" is not supported. The correct term is "Stacked case labels".
I apologize for the terminology mix-up.

Thanks,
Michael C., MCDBA
 
R

Richard Blewett [DevelopMentor]

LOL - I must have been in phonetic mode ;-)

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>


Also you are aloud to

should be "allowed to"



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.775 / Virus Database: 522 - Release Date: 08/10/2004



[microsoft.public.dotnet.languages.csharp]
 
A

ambshah

in vb we used this technique a lot its neat

switch(true)

{case (10 < 100):

MessageBox.Show("First ");

break;

case (10> 100):

MessageBox.Show("Second");

break;

default:

MessageBox.Show("default");

break;

}


substitute variable in place of 10

hth
ambrish
 
P

Paul E Collins

Jon Skeet said:
Is [VB's Case x To y] any better than
[if i ... else if i ...]
If so, why?

Well, I'll play devil's advocate: the 'if - else if' form makes you
repeatedly specify the variable being tested, which could lead to an
obscure bug if you accidentally specify a different variable at some
point.

P.
 
©

©tefan ©imek

Paul E Collins said:
Jon Skeet said:
Is [VB's Case x To y] any better than
[if i ... else if i ...]
If so, why?

Well, I'll play devil's advocate: the 'if - else if' form makes you
repeatedly specify the variable being tested, which could lead to an
obscure bug if you accidentally specify a different variable at some
point.

P.

On the other hand, if you have the variable mentioned on the top of the
switch and then you have 20 cases spread along four pages of code, you might
just wonder what your case 10-100: means.

Personally, I think that ranged cases allow for more bugs than multiple
if's.

Stefan
 

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

Top