-1

Visual Studio is telling me that I can't apply an or statement in my switch.

Can I even apply that to my switch at all?

         Second_Chance_2 = System.Console.ReadLine();

            switch (Second_Chance_2)
            {
                case "":                             
                        Console.WriteLine("Error");
                        break;

                case "Alpha Area" || "alpha area": 
                       System.Console.WriteLine("Now The Real Game Begins");
                        break;

                default:
                        Console.WriteLine("Error");
                        break;
            }
2
  • 2
    In future, please try to post a short but complete program demonstrating your problem. You've posted over 150 lines of badly-indented code, very little of which is relevant to the problem at hand. If you find you need to add extra words to your post, that's a good indication that you haven't given enough information about what you're trying to achieved - don't just type rubbish. Commented Aug 4, 2015 at 18:28
  • possible duplicate of How add "or" in switch statements? Commented Aug 5, 2015 at 9:18

2 Answers 2

1

This || is the logical or operator and can be applied only to boolean values or expression that can be evaluated to a boolean.

That being said, you can only write

a || b

when both a and b are of type bool or expression that are evaluated to a bool.

That being said, this

 case "Alpha Area" || "alpha area":

is not valid

You could achieve that you want like below:

case "Alpha Area":
case "alpha area":

We do this when we want to a switch to go the same branch for two or more different cases.

Sign up to request clarification or add additional context in comments.

Comments

1

You got the wrong syntax, this:

 case "Alpha Area" || "alpha area": 

should be:

 case "Alpha Area":
 case "alpha area": 

Alternatively you could also convert the switch string to lower case to have only 1 case statement:

 switch (Second_Chance_2.ToLowerInvariant())
 {
      ...
      case "alpha area":

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.