2
Reply

The Switch and goto statements: workaround to exceptions

Ed Z

Ed Z

May 9 2008 2:55 PM
2.6k

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


Answers (2)