Hi all,
I'm rather new at the C# language and need some help... I have been doing some examples from a book but I have developed further objects on the basic example codes, and I have so far stumbled upon 2 difficulties in the code.
1. Can I have nested Switch statements? I assume I should be able to nest but so far have not found any book or source that explicitly states it. This code will give me an error when compiling:
....
UsrInput = System.
Convert.ToInt16(System.Console.ReadLine());Switch(UsrInput)
{
case 1: //some code here
....
UsrInput2ndMenu = System.Convert.ToInt16(System.Console.ReadLine());
switch(UsrInput2ndMenu)
{
case 1: //code
break;
case 2: //code
break;
default: //code
break;
}
case 2: //repeat code above with other options for yet another class
case 3:
default:
}
I am creating class objects at some point during the case executions, can this be related? If indeed is possible to nest Switch statements, are there any code exceptions that shoulb be kept out of nested switch statements (class creations, calls of methods, etc.)? I can solve this problem using nested if else statements, but I would really like to replace it for switch statements, which make the code look cleaner.
2) How can I make a far jump to another part of a program, if a goto statement can only be used to exit from a nested statement, or be used inside loops to make small jumps? It happens that if after a lot of user questioning from the program, I want to go back to the 1st part of the menu, I wanted (again) to use a Switch statement like this, and make a goto jump from inside to go to the top of the program:
System.
Console.WriteLine("Do you wish to perform another operation? [Y/N]");UsrSel = System.Console.ReadLine();
Retry:
switch (UsrSel)
{
case "Y": goto case "y";
case "y": goto SelIsYes;
case "N": goto case "n";
case "n":
System.Console.WriteLine("Bye");
break;
default: System.Console.WriteLine("Enter Yes or No only [Y/N]:");
System.Threading.Thread.Sleep(1200);
System.Console.Clear();
goto Retry;
}
Note that if I use the goto label "Retry" it works normally and sends back to the top of the Switch statement, however if I try to jump using the SelIsYes label inside, it will show the error "No SelIsYes label exist in the goto domain".
Please explain the correct way to circumvent this situation, that is hot to perform a jump to the desired code location without restriction.
Thanks a lot for your help
-ZamoraE
Ed ZPosted May 12, 2008, 10:13 AM
Hi,
Well, I made some testing on the code, and found that what happened was basically that the nested Switch statement was inside a wrong bracket key arrangement {}, which was throwing the exception, thanks. However it remains a mistery to me how to perform a valid "jump" to another part of the program without restriction, either using a goto or any other type of control command. Do you know how to perform it safely? I know we must restrict the use of goto commands to avoid making the code confuse, but sometime you just need to apply it in order to simplify things out.
Thanks
Dipal ChoksiPosted May 10, 2008, 2:15 AM
Hi,
You should be able to use nested switch statements.. Could you post the compile error that you are getting?