Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, August 1, 2009 3:37 PM
As above... could this statement work ? case (0 - 500)
public void Switch(int num)
{
switch (num)
{
case (0-500):
// belong to 0-500;
break;
case (501-1000):
// belong to 501-1000;
break;
}
}
All replies (12)
Saturday, August 1, 2009 3:50 PM ✅Answered
You can't really do that with a switch.
You can do:
if (num >= 0 && num <= 500) { ........}
else if (num >= 501 && num <= 1000) {.............}John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
Saturday, August 1, 2009 4:00 PM ✅Answered
You can do it with small ranges of numbers like so:
switch (myInt)
{
case 0:
case 1:
case 2:
// do something
break;
etc...
}
But I definately wouldn't recommend that for 500 of em.
Saturday, August 1, 2009 4:01 PM ✅Answered
The answer is NO!
See the same question and answer here:
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/c46bee82-ac9a-4f15-bcc0-9e5baa557916/
VB can do it but not C#. People suggested using if/else.
Oh, by the way, (0 - 500) is an expression and
(0 - 500) = -500
John Chen -- See my team blog: http://blogs.msdn.com/vsdata
Saturday, August 1, 2009 4:59 PM ✅Answered
Hi,
After I see all this answers there is no comment they are right you can't, so the best solution With such large ranges it is easier to use if - else if statements.
Thanks
We are volunteers, if the reply help you mark it as your answer. thanks!!
My Blog
Saturday, August 1, 2009 5:24 PM ✅Answered | 3 votes
As long as it is a range with a constant interval, you can map the range to an integer:
int range = (num-1) / 500;
switch (range) {
case 0: break; // 1-500
case 1: break; // 501-1000
// etc...
}
if/else for non-constant intervals.
Hans Passant.
Saturday, August 1, 2009 3:48 PM
Have you tried it? Posting takes longer than testing in the IDE.
Saturday, August 1, 2009 4:00 PM
JohnWein, that doesn't work in C# unfortunately. The switch only deals with constants
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
Saturday, August 1, 2009 4:06 PM
JohnWein, that doesn't work in C# unfortunately. The switch only deals with constants
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
What doesn't work in C#? You can certainly write code that doesn't work in the IDE. Try it.
Saturday, August 1, 2009 4:13 PM
Read the documentation here on switch
switch (expression)
{
case constant-expression
:
statement
jump-statement
[default:
statement
jump-statement]
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
Saturday, August 1, 2009 4:36 PM
Read the documentation here on switch
switch (expression) { case constant-expression : statement jump-statement [default: statement jump-statement] }
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
If this post is for my benefit, read my original post in this thread. The OP should have been able to answer his question without posting it to a forum. If you need assistance start a new thread.
Saturday, August 1, 2009 5:08 PM
It wasn't directed at you JohnWein, it was meant for all, generally speaking.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
Tuesday, November 11, 2014 12:25 PM
As long as it is a range with a constant interval, you can map the range to an integer:
int range = (num-1) / 500;
switch (range) {
case 0: break; // 1-500
case 1: break; // 501-1000
// etc...
}if/else for non-constant intervals.
Hans Passant.
Good logic.