0

So I might have miss spelled the question but yet I can't really see other way to explain myself.

My question is: Can we change the number of case of a switch to respond to something like a list in a foreach loop and if we can't is there any other viable way of doing it?

To make it more clear I'll give an example:

List<string> strings = new List<string>();
//use foreach display all strings
string Choice = Console.ReadLine();
switch(Choice)

/* here instead of having static cases like
case "oneCase"
/do something
break;
*/

is it possible to have a

foreach(string str in strings)
{
case str:
//do something
break;
}

Well I did a bit of search before and I found that it is impossible because a case can't take a variable but is there any other way to have a "responsive switch" or is it totally impossible?

Thanks for reading me and for your answers and sorry if I'm unclear

Edit 1 : I'll use the switch to use different instance of an object: I'll will read from a file populate a list and then ask the user to choose one item from this list

Edit 2: Use a foreach to create a list of constant string is a good idea and I'll look at dictionary (never used them but it's always time to learn)

Edit 3: Here an example:

 static void Main(string[] args)
    {
        Student student;
        Module module = new Module();
        Begin:
        Console.WriteLine("Choose a student:");
        Console.WriteLine("1) Student 1");
        Console.WriteLine("2) Student 2");

        ConsoleKey StudentChoice = Console.ReadKey(true).Key;

        switch (StudentChoice)
        {
            case ConsoleKey.D1:
            case ConsoleKey.NumPad1:

            student = new Student();
            break;

            case ConsoleKey.D2:
            case ConsoleKey.NumPad2:
            student = new Student();
            break;

            default:
            Console.WriteLine("No Student Found");
            Console.ReadKey();
            Console.Clear();
            goto Begin;
        }
        ModuleChoice:
        Console.WriteLine("The student choosen is enrolled in this course: {0}", student.course);


        Console.WriteLine("Which Module do you want to look at?");

        ConsoleKey ModuleChoice = Console.ReadKey(true).Key;


        int i = 1;
        foreach (Module mod in student.course.Modules) //a course contain a list of module
        {
            switch(ModuleChoice)
                {
                    case ConsoleKey.D1: //here was trying to add more case depending the number of module in course
                    break;
                }
            i++;
        }

So to resume I have a list which doesn't have the same length all time and so I was trying to had to my switch the good number of case based on this class

Edit 4 : I start to look at dictionary and also take a look at this question: https://stackoverflow.com/a/42505360/2946329

Thanks everyone for your help I come back If I need more help about this

Lothandar
  • 3
  • 4

1 Answers1

4

If you intent to run an action based on a value entered by a user, you could use a dictionary with actions:

var d = new Dictionary<string, Action>();
d.Add("a", () => Console.WriteLine("You typed 'a'!");

if (d.TryGetValue(input, out Action action))
{
    action();
}

But if the list of actions can be statically defined, just use a switch:

switch (input)
{
    case "a":
        Console.WriteLine("You typed 'a'!");
        break;
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325