mercredi 8 mai 2019

Changing code from if-statement to switch-statment in c#

so i've never used switchcase instead of if/else if, and i am wondering how to go from if/else if to a switch-statement. 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 gotten it to work with an if-statement, put as I have understood, this takes up a lot of the computers prossesing powers(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";
        }

Aucun commentaire:

Enregistrer un commentaire