views:

96

answers:

5

Hello,

im doing an exercise where i have to make a parking lot application. The objectives are: Create a form with a numericUpDown control for choosingthe amount of time a vehicle was parked. Add option buttons to show if the vehicle is a car or truck. Clearly label each box and button. Add a label or text box to show the amount to pay.

Also, i have to set some limits. Cars shouldn't pay more than $38.00 for 24 hours, and trucks no more than $44.50 for 24 hours.

The only problem i find is that i cannot set these limits. It gives an error: Use of unassigned local variable 'result' .

Check the commented /// code.

Any help much appreciated. It's not a schoolwork project, i'm just learning c# by myself and doing some exercises.

Here's the code:

        private void calculate_Click(object sender, EventArgs e)
    {
        double carhours = 3;
        double truckhours = 3.50;
        double carFirsthour = 5;
        double truckFirsthour = 6;
        int hrs = (int)numericUpDown.Value;
        double result;
        int maxCarFee = 38;


        if (numericUpDown.Value < 1)
        {
            MessageBox.Show("NO WAY");
            return;
        }

        if (carButton.Checked == true && (numericUpDown.Value == 1))
        {
            result = (hrs * carFirsthour);
            fee.Text = "$" +  result;
        }

        if (carButton.Checked == true && (numericUpDown.Value > 1))
        {
            result = ((hrs - 1) * carhours + carFirsthour);
            fee.Text = "$" + result;
        }

        if (truckButton.Checked == true && (numericUpDown.Value == 1))
        {
            result = (hrs * truckFirsthour);
            fee.Text = "$" + result;
        }

        if (truckButton.Checked == true && (numericUpDown.Value > 1))
        {
            result = ((hrs - 1) * truckhours + truckFirsthour);
            fee.Text = "$" + result;
        }

        /// limits

        ///if (carButton.Checked == true && (numericUpDown.Value < 24) && (result > maxCarFee))
        ///{
        ///    fee.Text = "$" + maxCarFee;
        ///}
    }
+1  A: 

you need to convert numericUpDown.Value to int32

or becuase you did this:

int hrs = (int)numericUpDown.Value; 

you can just use hrs instead of numericUpDown.Value in the if statement

guy schaller
Yea, don't know why i missed that! Thanks.But the problem is with the 'result' variable.The code as it is right now gives an error: Use of unassigned local variable 'result'
Manolis
A: 

You check if numericUpDown.Value is smaller than 24, i think you mean is greater (>)?!

Use of unassigned local variable 'result'

means you do not initialise the variable. you have to do something like this

result = 0 

before you go to the if construct.

Sebastian
+1  A: 

Try something like this:

private void calculate_Click(object sender, EventArgs e) {
    double carhours = 3.0;
    double truckhours = 3.5;
    double carFirsthour = 5.0;
    double truckFirsthour = 6.0;
    double maxCarFee = 38.0;
    double maxTruckFee = 44.5;

    int hrs = (int)numericUpDown.Value;
    double result;

    if (hrs < 1) {
        MessageBox.Show("NO WAY");
        return;
    }

    if (carButton.Checked) {
        result = (hrs-1) * carhours + carFirsthour;
        if (result > maxCarFee) {
            result = maxCarFee;
        }
    }
    else {
        result = (hrs-1) * truckhours + truckFirsthour;
        if (result > maxTruckFee) {
            result = maxTruckFee;
        }
    }

    fee.Text = "$" +  result;
}

However, you don't really make it clear what should happen if a car/truck stays for more than 24 hours (or more than 48 hours). You might end up with more consistent results with something like this:

private void calculate_Click(object sender, EventArgs e) {
    double hourlyFee = 3.0;
    double firstHourFee = 5.0;
    double max24hrFee = 38.0;

    if (!carButton.Checked) {
       hourlyFee = 3.5;
       firstHourFee = 6.0;
       max24hrFee = 44.5;
    }

    int hrs = (int)numericUpDown.Value;
    if (hrs < 1) {
        MessageBox.Show("NO WAY");
        return;
    }

    double result = 0.0;
    if (hrs >= 24) {
       result = (hrs / 24) * max24hrFee;
       hrs = hrs % 24;
    }

    if (hrs > 0) {
       result = result + firstHourFee + ((hrs-1) * hourlyFee);
    }

    fee.Text = "$" +  result;
}
Tom
Nice clean up, except the "== true" can also be removed if it doesn't harm code readability. I type slowly, so every character counts :P
SirDemon
A: 
int hrs = (int)numericUpDown.Value;

You get value in hrs so you can use this in your if conditions.

if (carButton.Checked == true && (numericUpDown.Value == 1))

Write it like following. No need of == true

if (carButton.Checked && (numericUpDown.Value == 1))
Muhammad Kashif Nadeem
A: 

The problem is that none of the conditional (if) statements are executed so result is never assigned a value. Assign result in its declaration -- double result = 0D; -- and the code will run so you can debug the conditional logic.

Jamie Ide