pandapung
pandapung

Reputation: 11

Changing code from if-statement to switch-statement in C#

I've never used switch case instead of if/else if, and I'm wondering how to use it. I would really appreciate the help! The task is to put in an amount of wind in a textbox(tbVindstyrke) and the code should tell the user what amount of Watt per hour(W/t) the wind is generating, in a windmill. It should post the result in a label (lbWattprodusert).

I have got it to work with an if-statement, put as I have understood, this takes up a lot of the computers processioning power (or something). So, I would like to switch it up to a switch-statement.

double Vs = 0;
private void btSjekkW_Click(object sender, EventArgs e)
{
    Vs = Convert.ToDouble(tbVindstyrke.Text);
    if (Vs >= 0 && Vs <= 2.4)
        lbWattProdusert.Text = 0 + " W/t";
    else if (Vs >= 2.5 && Vs <= 3.3)
        lbWattProdusert.Text = 2 + " W/t";
    else if (Vs >= 3.4 && Vs <= 5.4)
        lbWattProdusert.Text = 10 + " W/t";
}

Upvotes: 1

Views: 85

Answers (3)

AbdelAziz AbdelLatef
AbdelAziz AbdelLatef

Reputation: 3744

switch doesn't work with float and double values. However, as all your range limits are multiples of 0.3, you can use the trick of dividing the value by 0.3 to achieve what you seek as follows:

int Vi;
double Vs = 0;
private void btSjekkW_Click(object sender, EventArgs e)
{
    Vs = Convert.ToDouble(tbVindstyrke.Text);
    Vi = (int)(Vs / 0.3);
    switch (caseSwitch)
    {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
            lbWattProdusert.Text = 0 + " W/t";
            break;
        case 9:
        case 10:
        case 11:
            lbWattProdusert.Text = 2 + " W/t";
            break;
        case 12:
        case 13:
        case 14:
        case 15:
        case 16:
        case 17:
        case 18:
            lbWattProdusert.Text = 10 + " W/t";
          default:
            // what to do if Vs > 5.4
            break;
      }
}

Upvotes: 0

Fenton
Fenton

Reputation: 250812

A switch statement is useful when you want to choose between many options based on a single value. When you want to work against ranges, as you have done in your example, an if-statement is needed.

Here's an example of a switch statement... you can see that you specify the value to switch on at the start and you can then perform different actions based on equality.

string color = "red";

switch (color)
{
    case "red":
        // do something
        break;
    case "green":
        // do something
        break;
    case "blue":
        // do something
        break;
    default:
        throw new ColorUnknownException(color);
}

Very often, a switch-statement is a sign of a missing design pattern... but that is a conversation for another day.

Upvotes: 0

Sach
Sach

Reputation: 10393

switch statements work with constant values.

So, this is valid:

var val = 2;
switch (val)
{
    case 1:
        // Do something if val is 1.
        break;
    case 2:
        // Do something if val is 2.
        break;
    default:
        // Do something for all values of val other than 1 or 2.
        break;
}

But you want to convert an if-else that deals with ranges. That too with double ranges. This is not possible since a switch doesn't allow you to work with a range.

If your ranges are int, you could hypothetically write a case for each value in the rage, but that makes absolutely no sense.

Say, you want to do something if the value is between int 1-3, and something else if it's between 4-6. You could write something like the follwing, but that would be nonsensicle. You'd be better off sticking to an if-else.

var val = 2;
switch (val)
{
    case 1:
    case 2:
    case 3:
        // Do something if val is between 1-3.
        break;
    case 4:
    case 5:
    case 6:
        // Do something if val is between 4-6.
        break;
    default:
        // Do something for all other values of val
        break;
}

Upvotes: 2

Related Questions