Search code examples
c#switch-statementtypeconverter

c# Switch case not working


I'm trying to check a value by using a switch:

private void btnInput1Rste_Click(object sender, EventArgs e)
{
    switch (sender == btnInput1Rste)
    {
        case "1": currentButtonPressedRste = 1;
            break;
    }
}

It gives the error as follows:

can't convert type 'string' to 'bool'

However, when i try to convert it to a boolean it says:

a constant value is expected.

How do I fix it?

When it works it's supposed to check 3 values. (not with just this switch)


Solution

  • sender == btnInput1Rste is a boolean expression; the result is either true or false. Frankly, you probably just want an if/else here. You probably can do a switch with case true: but...