user3425805
user3425805

Reputation: 13

C# Use of unassigned local variables (int)

I am wondering what I am doing wrong, as I am trying to make a menu, in which it needs to calculate three different courses together in predefined values, however I seem to be doing the conversions wrong, as I cant seem to make it work right.

    int a, b, c;
    textBox4.Text = (a + b + c).ToString();
    if (comboBox1.Text == "Tzatziki")
    {
    a = 35;
    }
    else if (comboBox1.Text == "Carpaccio")
    {
    a = 40;
    }
    else if (comboBox1.Text == "Bruscetta")
    {
    a = 30;
    }
    else if (comboBox1.Text == "Shrimp Cocktail")
    {
    a = 40;
    }
    if (comboBox2.Text == "Sirloin")
    {
    b = 225;
    }
    else if (comboBox2.Text == "Lamb")
    {
    b = 195;
    }
    else if (comboBox2.Text == "Salmon")
    {
    b = 170;
    }
    else if (comboBox2.Text == "Veggy")
    {
    b = 100;
    }
    if (comboBox1.Text == "Chocolate Cake")
    {
    c = 45;
    }
    else if (comboBox1.Text == "Pancakes")
    {
    c = 35;
    }
    else if (comboBox1.Text == "Waffles")
    {
    c = 40;
    }
    else if (comboBox1.Text == "Sundae")
    {
    c = 38;
    }

}

Would appreciate the advice, kinda new to C# programming :)

Upvotes: 0

Views: 93

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477284

Before you can convert an integer, to its textual counterpart, you need to give it an initial value.

The reason why you get zero is because after you initialize variables (with zero), you immediately use them. In that sence the if-then-else patterns are useless.


A possible fix is to use switch-case patterns to ease coding and after you calculated a, b and c, set the text value:

int a = 0, b = 0, c = 0;
switch(comboBox1.Text) {
    case "Tzatziki" :
        a = 35;
        break;
    case "Carpaccio" :
        a = 40;
        break;
    case "Bruscetta" :
        a = 30;
        break;
    case "Shrimp Cocktail" :
        a = 40;
        break;
}
switch(comboBox2.Text) {
    case "Sirloin" :
        b = 225;
        break;
    case "Lamb" :
        b = 195;
        break;
    case "Salmon" :
        b = 170;
        break;
    case "Veggy" :
        b = 100;
        break;
}
switch(comboBox3.Text) {
    case "Chocolate Cake" :
        c = 45;
        break;
    case "Pancakes" :
    c = 35;
        break;
    case "Waffles" :
        c = 40;
        break;
    case "Sundae" :
        c = 38;
        break;
}
textBox4.Text = (a + b + c).ToString();

You probably made even a mistake for c, since you used ComboBox1 again (I expect this should be ComboBox3. By using a switch-case structure, the code becomes more elegant.


A final problem with the code is what to do if no value has been entered in the combobox (or a different one from what you have checked). In the case I wrote above, the values will remain 0 (only the values corresponding to the ComboBox that has an unknown value). You can add a defaut : case in order to decide what to do then.

Upvotes: 1

Selman Genç
Selman Genç

Reputation: 101701

Give value to your variables and initialize them:

int a = 0, b = 0, c = 0;

And I think you need to move this line end of your if / else-if statements:

textBox4.Text = (a + b + c).ToString();

Upvotes: 2

Related Questions