switch or what?

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I want to test whether an int passed into a method is within various ranges.
In VBScript, this is pretty easy to do with a Select Case:

Select Case (myInt)
Case < 5000: i = 0
Case <10000: i = 10
Case <15000: i = 15
etc
End Select

C# doesn't seem to like this approach. What should I be doing?

Thanks
 
Mike said:
I want to test whether an int passed into a method is within various
ranges. In VBScript, this is pretty easy to do with a Select Case:

Select Case (myInt)
Case < 5000: i = 0
Case <10000: i = 10
Case <15000: i = 15
etc
End Select

C# doesn't seem to like this approach. What should I be doing?

Just use nested ifs:

if (myInt<5000) i=0;
else if (myint<10000) i=10;
else if (myint<15000) i=15;
//etc

Even if it's not as pretty as VBScript, once compiled it's just as
efficient, since VBScript has to execute a comparison for each of the cases
anyway.
 
Alberto Poblacion said:
Just use nested ifs:

if (myInt<5000) i=0;
else if (myint<10000) i=10;
else if (myint<15000) i=15;
//etc

Even if it's not as pretty as VBScript, once compiled it's just as
efficient, since VBScript has to execute a comparison for each of the
cases anyway.

LOL. That was my "temporary work-around"

:-)

Thanks
Mike
 
Mike said:
LOL. That was my "temporary work-around"

That will be your permanent workaround as well !

If your limits are as nice as in the example then you can make
a switch on myInt/5000, but I doubt that will be the case.

Arne
 
Back
Top