Info for switch statements
-
I was talking to a VB programmer a few days ago, and got to talking about switch/select statements, and how in VB you can do things like Case >10 but you can't in C#. Has anyone found a way around this at all? or do you have to fall back on multiple if statements?? or even worse, itemise every possible option that could come through the switch? Also, am I looking at this the wrong way - if so, what is the proper way to handle these kinds of situations?
-
I was talking to a VB programmer a few days ago, and got to talking about switch/select statements, and how in VB you can do things like Case >10 but you can't in C#. Has anyone found a way around this at all? or do you have to fall back on multiple if statements?? or even worse, itemise every possible option that could come through the switch? Also, am I looking at this the wrong way - if so, what is the proper way to handle these kinds of situations?
No, I'm afraid you can't do that with C#. The expressions in the case labels must be constant since they're evaluated at compile-time (for speed at run-time). To do what you want to do, you'd use a structure like
if (firstCondition) {
// do stuff
}
else if (secondCondition) {
// do some other stuff
}
else {
// otherwise if all fails do this
}Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.
-
No, I'm afraid you can't do that with C#. The expressions in the case labels must be constant since they're evaluated at compile-time (for speed at run-time). To do what you want to do, you'd use a structure like
if (firstCondition) {
// do stuff
}
else if (secondCondition) {
// do some other stuff
}
else {
// otherwise if all fails do this
}Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.
That works if you only have 3 or so conditions, but what happens if you have 30 conditions? How can you allow for so many responses without using a series of if-elseif statements? This is hypothetical really, but there are some situations where either through bad design, poor experience or even bad luck that you end up needing something like this
-
That works if you only have 3 or so conditions, but what happens if you have 30 conditions? How can you allow for so many responses without using a series of if-elseif statements? This is hypothetical really, but there are some situations where either through bad design, poor experience or even bad luck that you end up needing something like this
Then you have two choices, I suppose: either write out the 30 or so if-elseif lines, or, you take a step back and consider how to refactor the code into something cleaner. The switch statement won't help you, I'm afraid. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.
-
Then you have two choices, I suppose: either write out the 30 or so if-elseif lines, or, you take a step back and consider how to refactor the code into something cleaner. The switch statement won't help you, I'm afraid. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.