user4853171
user4853171

Reputation:

How to use a value in switch case?

I pretty new to C# programming so recently I was assigned to do a assignment to calculate the "AverageSpeed" and then match it against the "maxSpeed".

I have issue with the "maxSpeed" as no matter what value I key in I will get a "You are within the speed limit" where I know I should not have receive such a message. I suspect they are using the value of "maxSpeed" of 0 not the max speed in the case.

The program is all about requesting the user for the driver name, the type of vehicle and the travel distance with duration in minutes to find out whether driver has exceed the maximum allowable speed.

Please advise.

private void btnSubmit_Click(object sender, EventArgs e)
{
    int minutes = Convert.ToInt32(txtDuration.Text);
    double miles = Convert.ToInt32(txtDistance.Text);
    double hours = minutes / 60;
    double AverageSpeed = GetMPH(hours, miles);

    Object itemSelect = (Object)cboCarType.SelectedItem;
    string carType = itemSelect.ToString();

    int maxSpeed =0;
    switch (carType)
    {
        case "Sedan":
            maxSpeed = 100;
            break;
        case "Wagon":
            maxSpeed = 90;
            MessageBox.Show("Wagon");
            break;
        case "Truck":
            maxSpeed = 50;
            break;
        case "Van":
            maxSpeed = 60;
            break;
        case "Bus":
            maxSpeed = 50;
            break;
        case "Tractor trailer":
            maxSpeed = 30;
            break;
    }

    if (AverageSpeed > maxSpeed)
    {
        MessageBox.Show("You have exceed the maximun speed!");
    }
    else
    {
        MessageBox.Show("You are within the speed limit!");
    }

}

double GetMPH(double miles, double hours) 
{
    double GetMPH = miles / hours;
    return GetMPH;
}

Upvotes: 0

Views: 310

Answers (5)

Simon Bosman
Simon Bosman

Reputation: 41

Maybe you could print the carType in you code above the switch statement. For example

MessageBox.Show(carType);

This way you can check if the switch statement is working. e.g. is carType really a sedan as expected?

Debugging and setting a breakpoint is of course also possible.

Upvotes: 0

Krunal Mevada
Krunal Mevada

Reputation: 1655

You are done with below code :

int minutes = Convert.ToInt32(txtDuration.Text);
double miles = Convert.ToInt32(txtDistance.Text);
double hours = minutes / 60;
double AverageSpeed = GetMPH(hours, miles);

Replace your with below code:

double minutes = Convert.ToDouble(txtDuration.Text);
double miles = Convert.ToDouble(txtDistance.Text);
double hours = minutes / 60;
double AverageSpeed = GetMPH(miles, hours);

And make sure you get correct selected value in your string carType.

Hope this will help you.

Upvotes: 0

Kiran Varsani
Kiran Varsani

Reputation: 587

The problem is in your carType string which is not being set properly. Change itemSelect.ToString() to itemSelect.Text

private void btnSubmit_Click(object sender, EventArgs e)
{

    int minutes = Convert.ToInt32(txtDuration.Text);
    double miles = Convert.ToInt32(txtDistance.Text);
    double hours = minutes / 60;
    double AverageSpeed = GetMPH(miles, hours);

    Object itemSelect = (Object)cboCarType.SelectedItem;
    string carType = itemSelect.Text;

    int maxSpeed =0;
        switch (carType)
        {
            case "Sedan":
                maxSpeed = 100;
                break;
            case "Wagon":
                maxSpeed = 90;
                MessageBox.Show("Wagon");
                break;
            case "Truck":
                maxSpeed = 50;
                break;
            case "Van":
                maxSpeed = 60;
                break;
            case "Bus":
                maxSpeed = 50;
                break;
            case "Tractor trailer":
                maxSpeed = 30;
                break;
        }
         if (AverageSpeed > maxSpeed)
        MessageBox.Show("You have exceed the maximun speed!");
    else
        MessageBox.Show("You are within the speed limit!");

}

double GetMPH(double miles, double hours) 
{
    double GetMPH = miles / hours;
    return GetMPH;
}

UPDATE: You can also use following code to get carType.

//Object itemSelect = (Object)cboCarType.SelectedItem; //Remove this line
string carType = cboCarType.SelectedItem.Text;

And change your GetMPH function call to GetMPH(miles, hours)

Upvotes: 1

Simon Bosman
Simon Bosman

Reputation: 41

Probably the switch statement does not update the maxSpeed to the selected value. Place a breakpoint at the switch statement to see the value of carType.

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180777

Step through the code and find out what the values for AverageSpeed and maxSpeed are. I suspect, based on the description of your error, that AverageSpeed is always zero.

Change your switch to a function, like this:

public int MaxSpeed(string carType) 
{
   switch (carType)
    {
        case "Sedan": return 100;
        case "Wagon": return 90;
        case "Truck": return 50;
        case "Van": return 60;
        case "Bus": return 50
        case "Tractor trailer": return 30;
    }
    return 0;
}

This will allow you to unit test the function separately and prove that it works.

Upvotes: 1

Related Questions