简体   繁体   中英

how do i use an array in a switch statement in c#?

So i am new to programming so im pretty confused about this. I created an array and tried to use it inside of a switch statement:

string[] General = new string[5];
{
    General[0] = "help";
    General[1] = "commands";
    General[2] = "hello";
    General[3] = "info";
    General[4] = "quit";
}

switch(General)
{
    case 0:
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("This is a new program. Therefore the amount of commands are limited. \nIt can do simple things. For example, if you say 'tell the time' then it will tell the time\n");
            Console.ForegroundColor = oldColor;
            continue;
        }
}

As far as i am aware there are no problems with this. However, when i run the code i am greeted with this error : "A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type"

I am genuinely stuck with this and i cant find any answers on the internet so any help will be greatly appreciated. Thanks

You are doing the switch statement on the entire array, opposed to a single entry in the array.

Assuming you are trying to write all of the available inputs you could do

    string[] General = new string[5];
    {
        General[0] = "help";
        General[1] = "commands";
        General[2] = "hello";
        General[3] = "info";
        General[4] = "quit";
    }

foreach(var option in General)
{
    switch(option)
    {
        case "help":
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("This is a new program. Therefore the amount of commands are limited. \nIt can do simple things. For example, if you say 'tell the time' then it will tell the time\n");
                Console.ForegroundColor = oldColor;
                break;
            }
        case "commands":
            {
                //Do some stuff
                break;
            }
        //etc etc
    }
}

It sounds like what you are looking for is an enum .

public enum General {
    help = 0,
    commands = 1,
    hello = 2,
    info = 3,
    quit = 4
}

Then you can use a switch statement just fine :).

// variable to switch
General myGeneral;

// myGeneral is set to something

switch(myGeneral)
{
    case General.help:
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("This is a new program. Therefore the amount of commands are limited. \nIt can do simple things. For example, if you say 'tell the time' then it will tell the time\n");
        Console.ForegroundColor = oldColor;
        break;
}

The parameter in the switch statement should be the user input, not your optional values, for example:

int input = 0; // get the user input somehow
switch (input)
{
    case 0: 
    {
        // Do stuff, and remember to return or break
    }
    // Other cases
}

Also, this is a perfect use case for an Enum . That would look something like this:

public enum General 
{
    HELP = 0,
    COMMANDS = 1,
    HELLO = 2,
    INFO = 3,
    QUIT = 4
}

int input = 0; // get the user input somehow
switch (input)
{
    case General.HELP: //Notice the difference?
    { 
        // Do stuff, and remember to return or break
    }
    // Other cases
}

This makes your intention very clear, and therefore makes your code more readable and more maintainable. You can't do this with your array, because even though you declare your array in your code, it is still variable and therefore its state at the switch statement is not known at compile time. Enum s are immutable, and therefore their values are known at compile time and can be used in switch statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM