Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listbox unassigned variable

Tags:

c#

listbox

Full disclosure here, I am a student doing homework. I have 2 listboxes with items that can be selected. What is said in them is not needed to be extracted. I wrote the code out and everything works except I get an error saying "use of unassigned variable" on 3 variables at the end of the code. They are locFees, days, and registration. Can anyone tell me what I am doing wrong that is causing the variables to not have a value?

    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;
    }
like image 664
user1689574 Avatar asked Sep 21 '12 18:09

user1689574


1 Answers

Can anyone tell me what I am doing wrong that is causing the variables to not have a value?

Sure - you're ignoring the possibility that workshopIndex isn't 0, 1, 2, 3 or 4.

If you believe that should never happen, just add:

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

Or if you just want to use some defaults, do something like:

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

That's the first way you can end up with days and registration unassigned.

Next, there's the fact that you only go into the switch block at all if lbWorkshop.SelectedIndex != -1. Your else block is just:

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

... so after that else block, you're going to continue. You probably want:

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

You've then got the same problem for locFees, in terms of both the switch statement and the else block.

One thing to learn from this: be grateful that the compiler spotted these for you. It's stopped you from running code which definitely had bugs in. That's always a good thing.

like image 140
Jon Skeet Avatar answered Nov 13 '22 04:11

Jon Skeet