1

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

9
  • You can't switch on an array. Perhaps you meant to use an enum Commented Jul 6, 2015 at 19:48
  • is this supposed to be reading user input on something? or is it suppose to just display all the options available? Commented Jul 6, 2015 at 19:50
  • It's not really clear what you're trying to "switch" on. Do you want to loop over the array and switch on each value? Commented Jul 6, 2015 at 19:50
  • @ryanyuyu thanks. Ill find out what enums do Commented Jul 6, 2015 at 19:52
  • 1
    Consider using commandline.codeplex.com Commented Jul 6, 2015 at 19:54

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Worth noting, is that the values of the enum increment by 1 by default. So as long as you set the initial value help=0 everything else will auto-increment.
1

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
    }
}

Comments

0

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. Enums are immutable, and therefore their values are known at compile time and can be used in switch statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.