iascaire
iascaire

Reputation: 11

Invalid expression in Case Switch

This has been Driving me crazy all day.

private void BtnMix_Click(object sender, EventArgs e)
{
   //Declare the variables to be used in the process
    String Color1;
    String Color2;

    //Determine whether or not a selection has been bade
    if (Colorlist1.SelectedIndex != -1 && ColorList2.SelectedIndex != -1) 
    {
        //Get the selected colors
        Color1 = Colorlist1.SelectedItem.ToString();
        Color2 = ColorList2.SelectedItem.ToString();

        //Determine the correct result
        switch (Color1 +|+ Color2)
        {
            case "Red|Red":
                lblResult.Text = "Red";
                break;
            case "Red|Blue":
                Lblresult.text ="Purple";
                break;
            case "Red|Yellow":
                lblresult.text = "Orange";
                break;
            case "Blue|Red":
                lblresult.text = "Purple";
                break;
            case "Blue|Blue":
                lblresult.text = "Blue";
                break;
            case "Blue|Yellow":
                lblresult.text = "Green";
                break;
            case "Yellow|Red":
                lblresult.text = "Orange";
                break;
            case "Yellow|Blue":
                lblresult.text = "Green";
                break;
            case "Yellow|Yellow":
                lblresult.text = "Yellow";
                break;
        }
    }
    else
    {
        MessageBox.Show("Please select two colors");
    }
}

I know there's something incredibly easy I'm missing, but for the life of me I can't figure it out. I've managed to make everything "work" so far, but this last error just doesn't seem to make any sense. "Invalid expression for the term '|' " on line 15.

Upvotes: 0

Views: 979

Answers (2)

Mohd samee
Mohd samee

Reputation: 1

Its showing invalid expression term switch class Program { static void Main() { int Totalcoffeecost = 0; string Userdecision = string.Empty; do { int userchoice =-1; do {

        Console.WriteLine("Please enter your coffee size: 1-small, 2-Medum,3-Large");
       userchoice = int.Parse(Console.ReadLine());
            }while(





     switch (userchoice)
        {
            case 1:
                Totalcoffeecost += 1;
                break;
            case 2:
                Totalcoffeecost += 2;
                break;
            case 3:
                Totalcoffeecost += 3;
                break;
            default:
                Console.WriteLine("Your choice {0} is invalid", userchoice);
                break;
        } 
        while(userchoice !=1 && userchoice !=2 && userchoice !=3)
            do
            {
                Console.WriteLine("Do you want to buy another coffee-Yes or No");
                string userdecision = "";
                userdecision = Console.ReadLine().ToUpper();
                if(userdecision != "Yes" && userdecision !="No")

                {
                    Console.WriteLine("your choice {0} is invalid. Please try again",userdecision);
                }
            }while(Userdecision != "Yes" && Userdecision != "No");
            }
        while(Userdecision.ToUpper()!="No");
        Console.WriteLine("Thank you for shopping with us");
        Console.WriteLine("Bill amount={0}",Totalcoffeecost );


        }
        }

Upvotes: -1

Vishal
Vishal

Reputation: 6368

If you are willing to concatenate the strings then please use

Color1 + "|" + Color2

So, now your code should look like:

switch (Color1 + "|" + Color2)

Also, you will get another error on line No.21 because you used uppercase "L" instead of lowercase "l" in Lblresult

Upvotes: 4

Related Questions