Skip to content

Switch Case Options

shah

Asked Apr 4, 2007, 4:06 AM EDT3 answers
Share
LinkedInX
Hi If i have switch statement look like this and after getting or display function or by entering wrong character(default) how can i go to same screen which will display different options. As i know there are goto statements but that is not recommended? So please can anybody recommend me what to use or to do?
CSS
[code]
switch (s)
            {
                case "g":
                uname.getUserName();
                break;
    
                case "v":
                uname.DisplayUserName();
                break;
               
                default:
                Console.WriteLine("Please select correct choice...");
[/code]
Thanks

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    using System;

    public class CorrectChoice

    {

    public static void Main()

    {

    string s;

    Select uname = new Select();

    while(true)

    {

    Console.Write("Please select choice ");

    s = Console.ReadLine();

    switch(s)

    {

    case "g":

    uname.getUserName();

    break;

    case "v":

    uname.DisplayUserName();

    break;

    default:

    Console.WriteLine("Please select correct choice...");

    break;

    }

    if(s=="g" || s=="v")goto stop;

    }

    stop: Console.WriteLine();

    }

    }

    class Select

    {

    public void getUserName()

    {

    Console.WriteLine("selection is correct");

    }

    public void DisplayUserName()

    {

    Console.WriteLine("selection is correct");

    }

    }

    OR

    C#
    using System;
    public class CorrectChoice
    {
    public static void Main()
    {
    string s="";
    Select uname = new Select();
    while(s!="g" || s!="v")
    {
    Console.Write("Please select choice ");
    s = Console.ReadLine();
    switch(s)
    {
    case "g":
    uname.getUserName();
    break;
    
    case "v":
    uname.DisplayUserName();
    break;
    
    default:
    Console.WriteLine("Please select correct choice...");
    break;
    }
    }
    
    }
    }
    class Select
    {
    public void getUserName()
    {
    Console.WriteLine("selection is correct");
    }
    public void DisplayUserName()
    {
    Console.WriteLine("selection is correct");
    }
    }



    Thank you, Maha

    0
  • 0
    C#
    
    Hello,
    
    You could do a do...while(...) statement around your switch statement and keep track of your current "menu level".  It could look like this :
    
    bool continue = false;
    int menuLevel = 0;
    do
    {
        ShowMenu(menuLevel);
        switch (s)
        {
            case "g":
                uname.getUserName();
            break;
        
            case "v":
                uname.DisplayUserName();
            break;
                   
            default:
                Console.WriteLine("Please select correct choice...");
                continue = true;
                menuLevel++;
        }
    
    } while (continue);
    
    and in another function...
    private int ShowMenu(int menuLevel)
    {
       switch (menuLevel)
       {
           case 1:
               // display menu 1
               break; 
           case 2:
              // display menu 2
               break;
           default:
              // and so on...
       }
    }
    
    Also, the menuLevel should be an enumerated member (for code readability).
    
    Hope this helps!
    Simon
    0
  • 0
    THANKS A MILLION GUYS. IT HELPED A LOT
    0