-2

While this does seem like a very common question, I have yet to see an answer for C#. Most answers I have seen recommend using the eval() function but from what I understand, that function is exclusive to JavaScript with no visible equivalent to C#.

In my code, I thought I could use a switch statement like this:

void Update()
{

        int1 = 1;
        int2 = 2;
        op = "+";

        switch(op)
        {
            case "+":
                return output = int1 + int2;
            case "-":
                return output = int1 - int2;
            case "/":
                return output = int1 / int2;
            case "*":
                return output = int1 * int2;
        }
}

But that just resulted in an error:

 error CS0127: Since 'EquateNumb.Update()' returns void, a return keyword must not be followed by an object expression
M.U.S.H.A.
  • 19
  • 2
  • Is `output` defined somewhere else (e.g. as a class member)? If so, you probably just need to remove the return statements. Or if you actually need to exit inside the case, put the return on it's own line after you set `output` – devNull Aug 08 '20 at 15:02
  • 1
    Or if you actually want to store an operation in a variable to be used later, you're looking for delegates (`Action` and `Func<>`): https://stackoverflow.com/q/25676074/5803406 – devNull Aug 08 '20 at 15:05
  • Output is defined above Update as a public integer. However, removing return brings up error CS8070: Control cannot fall out of switch from final case label ('case "*":') – M.U.S.H.A. Aug 08 '20 at 15:06
  • https://stackoverflow.com/q/53128565/5803406 – devNull Aug 08 '20 at 15:07
  • First a case with the assignments, then a return output. You will need the silly breaks as in any case statement; a moronic legacy from c and c++ and also a reason to avoid cases.. – TaW Aug 08 '20 at 15:08
  • If you're using C#8, you can use the less verbose [switch expression](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression#basic-example) – devNull Aug 08 '20 at 15:41
  • void should only be used when you did not return anything. – Willy satrio nugroho Aug 09 '20 at 08:21

2 Answers2

0

Care about your declaration, TYPE Name, im not sure but you cant assign a variable name as a number!

You should care about the return of your fonction, void return nothing, like the void, check tutorial you will understand

int Update()
{

    int a = 1;
    int b = 2;
    op = "+";

    switch(op)
    {
        case "+":
            return (a + b);
        case "-":
            return (a - b);
        case "/":
            return (a / b);
        case "*":
            return (a * b);
    }
}
nerap
  • 226
  • 1
  • 2
  • 15
0

Moved my comment here because there's 2 parts to this.

  1. Your method has a return type of void which means the method will not return anything. This is obviously not what you want to do. So you will need to change void Update() to int Update().

  2. You are trying to assign an undeclared variable a value and return it at the same time. This is not possible. If you should refactor in 1 of 2 ways below.

Refactor 1: Removing output entirely

int Update()
{
        int1 = 1;
        int2 = 2;
        op = "+";

        switch(op)
        {
            case "+":
                return int1 + int2;
            case "-":
                return int1 - int2;
            case "/":
                return int1 / int2;
            case "*":
                return int1 * int2;
        }
}

Refactor 2: Use output

int Update()
{
        int1 = 1;
        int2 = 2;
        op = "+";
       //this line can be removed if output is declared else where
        int output = 0;

        switch(op)
        {
            case "+":
                output = int1 + int2;
                break;
            case "-":
                output = int1 - int2;
                break;
            case "/":
                output = int1 / int2;
                break;
            case "*":
                output = int1 * int2;
                break;
        }
        return output;
}

Side note:

  • Assuming int1 and int2 are declared outside this method, by assigning them values in this method, they will always be 1 and 2 whenever this method is called. This is the same case with op. It will always be + when this method is called.
li223
  • 369
  • 3
  • 11