I've got a large routine that runs off of a switch statement.
If one of the switches in switch #1 is true, that enters switch statement #2.
Some of the statements in switch #2 enter a 3rd switch.
I don't receive any compile errors except whenever I attempt to add default
switches to switches 2 or 3.
Am I nesting too deep, or do I need to call the deeply nested switches in a
special way?
If this is all fine and dandy in C#, then I need to look harder for errors
in my code.
Hi,
I would refactor that in such a way that each switch case call a
method. It makes the code more simple to read
"jp2msft" <jp2...@discussions.microsoft.com> wrote in message
news:281B645A-26E7-41F8...@microsoft.com...
"gerry" wrote:
> start looking
>
Yes, but that rarely results in readable code. As Ignacio suggested,
separate the nested switch statements into separate methods.
--
Jon Skeet - <sk...@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
FYI: You've got to have a "break;" listed after the default case if you are
nesting multiple cases.
You just have a single case statement? Probably don't have to stick a break
after the default case.
You always have to exit from the code following a case label, including
default and including the last one. That can be done in three ways:
1. Use break to exit out of the switch.
2. Use return to exit out of the method.
3. Use goto to jump into the code of another label.
However, if you don't have any code after the case label, you can skip
into the code of next case label.
Example:
swtich (code) {
case 1:
// no code here, and no break, so it skips into case 2
case 2:
Console.WriteLine("1 or 2");
break; // break needed
case 3:
break; // does not skip into the next case
default:
Consolse.WriteLine("other than 1, 2 or 3");
break; // break needed
}
> You just have a single case statement? Probably don't have to stick a break
> after the default case.
>
> "jp2msft" wrote:
>
>> Can switch statements be nested?
>>
>> I've got a large routine that runs off of a switch statement.
>>
>> If one of the switches in switch #1 is true, that enters switch statement #2.
>>
>> Some of the statements in switch #2 enter a 3rd switch.
>>
>> I don't receive any compile errors except whenever I attempt to add default
>> switches to switches 2 or 3.
>>
>> Am I nesting too deep, or do I need to call the deeply nested switches in a
>> special way?
>>
>> If this is all fine and dandy in C#, then I need to look harder for errors
>> in my code.
--
Göran Andersson
_____
http://www.guffa.com
> You always have to exit from the code following a case label, including
> default and including the last one. That can be done in three ways:
>
> 1. Use break to exit out of the switch.
> 2. Use return to exit out of the method.
> 3. Use goto to jump into the code of another label.
4. Throw an exception.
Eq.
5. Don't actually have an exit from the code following a case label -
just don't make the code at the end of the case reachable. For
instance, this is legal:
switch (i)
{
case 0:
while(true) {}
case 1:
break;
}
Hadn't actually thought about that before. Not really useful, I
guess...
Right. I didn't think of alternative 4, although I use it on a regular
basis. It's useful for handling enum values that should not exist:
switch (someEnumValue) {
case SomeEnum.This: DoSomething(); break;
case SomeEnum.That: DoSomethingElse(); break;
default:
// should never get here
throw new NotImplementedException();
}
I've never used alternative 5, and I can't imagine that I ever will... :)
Other have already told you that:
1) you can nest switches
2) you should consider creating methods to make the code more readable
I will just described what I have wished for in 20 years: multi value
switch statements.
To write:
switch(a)
{
case 1:
switch(b)
{
case 1:
Console.WriteLine("a=1 b=1");
break;
case 2:
Console.WriteLine("a=1 b=2");
break;
}
break;
case 2:
switch(b)
{
case 1:
Console.WriteLine("a=2 b=1");
break;
case 2:
Console.WriteLine("a=2 b=2");
break;
}
break;
}
as:
switch([a,b])
{
case [1,1]:
Console.WriteLine("a=1 b=1");
break;
case [1,2]:
Console.WriteLine("a=1 b=2");
break;
case [2,1]:
Console.WriteLine("a=2 b=1");
break;
case [2,2]:
Console.WriteLine("a=2 b=2");
break;
}
Arne
You know, what you actually want is called "pattern matching". Try F#
some day:
match (a, b) with
| (1, 1) -> Console.WriteLine("a=1 and b=1")
| (1, 2) -> Console.WriteLine("a=1 and b=2")
| (0, _) | (_,0) -> Console.WriteLine("a=0 or b=0")
| (x, y) when (x = y+1) -> Console.WriteLine("a = b + 1")
...