Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Try Catch Block Errors 1

Status
Not open for further replies.

OMGNinja

Programmer
Apr 1, 2010
9
US
Ok so I am trying to use a try catch to handle error in this simple gas mileage calculator. When I enter some number that are not in the error handeling like
gas = 8.97
begning miles = 853
end miles = 1235
it gives the right result so thats fine, but when I enter 0 for gas or for end miles it retures infinity.
also when I try to see if it will catch negative numbers or if begining miles is larger than end mileage it goes about the calculation anyway regardless of what the code wants to do.What is wrong with this?
Code:
try
            {
                float gas = float.Parse(textBox1.Text);
                float bMile = float.Parse(textBox2.Text);
                float eMile = float.Parse(textBox3.Text);

                //if gas or endmiles is 0
                if (gas == 0 )
                {
                    this.label6.Text = "Gallons is zero ";
                }
                if(eMile == 0)
                {
                    this.label6.Text = "Ending Mileage is zero ";
                }

                //see if begining mileage is larger than ending mileage
                if (bMile >= eMile)
                {
                    this.label6.Text = "End mileage less than begin mileage ";
                }

                //if negative values are entered
                if (gas < 0)
                {
                    this.label6.Text = "Gallons is negative  ";
                }

                if (bMile < 0)
                {
                    this.label6.Text = "Begning Miles is negative  ";
                }

                if (eMile < 0)
                {
                    this.label6.Text = "Ending Miles is negative  ";
                }

                this.label6.Text = String.Format("Result: {0}", (eMile - bMile) / gas);
            }
            catch (FormatException ex)
            {
                // string was not a number, handle the error or just use a formatted text box,   
                // NumericUpDown, etc.
            }
 
I would make the following changes.
1. provide descriptive names for the controls. lable6 and Textbox1 doesn't tell anyone anything about it's purpose.
2. move the calculation to it's own class
Code:
class MilageCalculator
{
  public float Beginning {get;set;}
  public float Ending {get;set;}
  public float Gas {get;set;}

  public float Calculate()
  {
     if (Gas == 0) return 0;
     return (Ending - Begining) / Gas;
  }
}
3. parse the values before using the calculator
Code:
float begin;
float end;
float gas;
if(float.TryParse(BegingingMilage.Text, out begin) == false) return;
if(float.TryParse(EndingMilage.Text, out end) == false) return;
if(float.TryParse(Gas.Text, out gas) == false) return;
var calculator = new MilageCalculator
                      {
                         Beginning = begin,
                         Ending = end,
                         Gas = Gas,
                      }
var result = calculator.Calculate();
Result.Text = string.Format("Result: {0}", result);
4. provide better validation
I believe there are UI validation controls you can add to the form so validation is separate from the actual processing.
Personally I would just validate the value is >= 0 and Beginning <= Ending. does the user or system really care if the number is zero or negative? Doesn't appear to; only that the number is a positive and Ending > Beginning.


Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks that help some and yeah I got lazy about nameing thing in C#
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top