0

I have a problem with my switch statement. I don't know how to use it properly in c#. It gives me the error CS0150 and I don't know how to care with that. I appreciate any help. Thank you. The code is cut. If you see less brackets than normal, please don't remind me.

int e = 0;
string caseslol;
string multi = "multi";

Console.WriteLine("Hi there!");
Console.WriteLine("Do you want to write a number so I can do cool things with it ??");
Console.WriteLine("Write yes to say YES and no to say NO");
string str1 = Console.ReadLine();
if (str1.Contains("yes") == true)
{
    Console.WriteLine("Enter it please");
    e = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("lol, we got it!");
    Console.WriteLine("you wrote this : {0}", e);
    Console.WriteLine("Now that we got it, we can do cool stuff!");
    Console.WriteLine("Do you want to know what is this stuff ???");
    string yesorno = Console.ReadLine();
    if (yesorno.Contains("yes") == true)
    {
        Console.WriteLine("OK");
        Console.WriteLine("We can do calculating stuff with your number");
        Console.WriteLine("Soo much cool, right ???");
        string yesorno2 = Console.ReadLine();
        if (yesorno2.Contains("yes"))
        {
            Console.WriteLine("Choose first another number and write it");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("So let's start with what ???");
            switch (caseslol) 
            {
                case multi:
                    Console.WriteLine(e * b);
            }
        }
    }

//this is the output :

error CS0150:A constant value is expected
Compilation failed: 1 error(s), 0 warnings
compiler exit status 1
derHugo
  • 83,094
  • 9
  • 75
  • 115
minus101
  • 3
  • 1
  • 2
  • If you want to use a string in a `switch` it have to be a constant string, i.e., `const string multi = "multi"` or use `"multi"` directly at the `switch's case` statement. – dcg Jun 13 '19 at 21:10

2 Answers2

1

You can't use a variable like that for the case, and instead must use a constant.

So instead of putting the variable multi in your case statement, you could use the string "multi" (which is the value you've assigned the variable anyway).

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
  • okay now i understand thank you but now it gives me this error : error CS8070:Control cannot fall out of switch statement through final case label `case "multi":' – minus101 Jun 13 '19 at 21:08
  • @HamzaTout you need to add a break; to the end of the case. – Owen Pauling Jun 13 '19 at 21:12
0

case can only be used with a constant. It should either be

private const string multi = "multi";

switch (caseslol)
{
    case multi:
        Console.WriteLine(e * b);

        // you also need this
        break;
}

or you do directly

switch (caseslol)
{
    case "multi":
        Console.WriteLine(e * b);

        // you also need this
        break;
}

or you simply use

if(string.Equals(caseslol, multi))
{
    Console.WriteLine(e * b);
}

in general I don't see where caseslol ever gets set ...

derHugo
  • 83,094
  • 9
  • 75
  • 115