Invalid list variable

Full disclosure here, I am a student doing homework. I have 2 lists with items that can be selected. What is said in them does not need to be extracted. I wrote the code and everything works, except that I get the error message "use of an unassigned variable" for 3 variables at the end of the code. These are locFees, days and registration. Can someone tell me what I am doing wrong, which leads to the fact that the variables do not matter?

    private void btnCalc_Click(object sender, EventArgs e)
    {
        double registration, lodging, total, days, locFees;
        int workshopIndex, locationIndex;
        if (lbWorkshop.SelectedIndex != -1)
        {
            workshopIndex = lbWorkshop.SelectedIndex;
            switch (workshopIndex)
            {
                case 0:
                    days = 3;
                    registration = 1000;
                    break;
                case 1:
                    days = 3;
                    registration = 800;
                    break;
                case 2:
                    days = 3;
                    registration = 1500;
                    break;
                case 3:
                    days = 5;
                    registration = 1300;
                    break;
                case 4:
                    days = 1;
                    registration = 500;
                    break;
            }
        }
        else
        {
            MessageBox.Show("You didn't select a workshop.");
        }
        if (lbLocation.SelectedIndex != -1)
        {
            locationIndex = lbLocation.SelectedIndex;
            switch (locationIndex)
            {
                case 0:
                    locFees = 150;
                    break;
                case 1:
                    locFees = 225;
                    break;
                case 2:
                    locFees = 175;
                    break;
                case 3:
                    locFees = 300;
                    break;
                case 4:
                    locFees = 175;
                    break;
                case 5:
                    locFees = 150;
                    break;
            }
        }
        else
        {
            MessageBox.Show("You didn't select a city.");
        }
        lodging = locFees * days;
        total = registration + lodging;
    }
+5
source share
1 answer

Can someone tell me what I am doing wrong, which leads to the fact that the variables do not matter?

Of course, you are ignoring the possibility that it is workshopIndexnot 0, 1, 2, 3, or 4.

, , :

default:
    throw new InvalidOperationException("Invalid selected index " + workshopIndex);

, , :

default:
    days = 1;
    registration = 100;
    break;

, days registration .

, , , lbWorkshop.SelectedIndex != -1. else :

 else
 {
     MessageBox.Show("You didn't select a workshop.");
 }

... else . , :

 else
 {
     MessageBox.Show("You didn't select a workshop.");
     return;
 }

locFees, switch, else.

, : , . , . .

+5

All Articles