Question Help with Try Catch statements

jflundstedt

New member
Joined
Sep 1, 2008
Messages
1
Programming Experience
Beginner
I am trying to write try/catch statements in my program. My program compiles and when I enter a 0 I do get the message box then I get an error that the input string is not in the correct format. Here is my code. My apologies if I posted anything incorrectly

VB.NET:
Public Class frmCompareGasMileage

    Private Sub frmCompareGasMileage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Threading.Thread.Sleep(5000)

    End Sub

    Private Sub cboYrsVehicleOwnership_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboYrsVehicleOwnership.SelectedIndexChanged
        'This event handler allows the user to enter the yearss of vehicle ownership from 1-10 years

        Dim intYrsOwned As Integer

        intYrsOwned = Me.cboYrsVehicleOwnership.SelectedIndex()

    End Sub

    Private Sub btnCompareLifeVehicle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompareLifeVehicle.Click
        'This event handler displays the Fuel cost for an SUV and Compact Car and the difference in cost
        'Calculation is as follows
        'miles traveled per year / mpg = gallons per year * yrs vehicle owned = # of gallons * fuel cost = total fuel cost

        'declare variables
        Dim intYrsOwned As Integer
        'Dim decFuel As Decimal
        'Dim strFuelCost As String
        Dim decFuelCost As Decimal
        Dim decMilesTraveled As Decimal
        Dim decMilesPerGallonSuv As Decimal
        Dim decMilesPerGallonCompact As Decimal
        Dim decCalcFuelCostSuv As Decimal
        Dim decCalcFuelCostCompact As Decimal
        Dim blnFuelCostIsValid As Boolean = False

        blnFuelCostIsValid = validateInput()

        If blnFuelCostIsValid Then
            decFuelCost = Convert.ToDecimal(Me.txtFuelCost.Text)
        End If

        intYrsOwned = Convert.ToInt32(Me.cboYrsVehicleOwnership.Text)
        decFuelCost = Convert.ToDecimal(Me.txtFuelCost.Text)
        decMilesTraveled = Convert.ToDecimal(Me.txtMilesTraveled.Text)
        decMilesPerGallonSuv = Convert.ToDecimal(Me.txtSuvMpg.Text)
        decMilesPerGallonCompact = Convert.ToDecimal(Me.txtCompactMpg.Text)
        decCalcFuelCostSuv = ComputeFuelCost(decMilesTraveled, decMilesPerGallonSuv, intYrsOwned, decFuelCost)
        decCalcFuelCostCompact = ComputeFuelCost(decMilesTraveled, decMilesPerGallonCompact, intYrsOwned, decFuelCost)
        Me.txtSuvGasCost.Text = decCalcFuelCostSuv.ToString("C")
        Me.txtCompactGasCost.Text = decCalcFuelCostCompact.ToString("C")
        Me.txtDifferenceCost.Text = decCalcFuelCostSuv.ToString("C2") - decCalcFuelCostCompact.ToString("C2")

    End Sub


    Private Function ComputeFuelCost(ByVal decMilesTraveled As Decimal, ByVal decMilesPerGallon As Decimal, ByVal intYrsOwned As Integer, ByVal decFuelCost As Decimal) As Decimal
        'Calculation is as follows
        'miles traveled per year / mpg = gallons per year * yrs vehicle owned = # of gallons * fuel cost = total fuel cost

        Dim decTotalFuelCost As Decimal

        decTotalFuelCost = ((decMilesTraveled / decMilesPerGallon) * intYrsOwned) * decFuelCost

        Return decTotalFuelCost

    End Function

    Private Function validateInput() As Boolean
        'this procedures validates the values entered

        'Dim strYrsOwned As String
        Dim decFuelCost As Decimal
        Dim strValidityCheckErrorMessage As String = "Please enter valid number"
        Dim strMessageBoxTitle As String = "Error"
        'Dim intYrsOwned As Integer
        Dim blnValidityCheck As Boolean = False

        Try
            decFuelCost = Convert.ToDecimal(Me.txtFuelCost.Text)
            If decFuelCost > 0 Then
                blnValidityCheck = True
            Else
                MessageBox.Show(strValidityCheckErrorMessage, _
                                strMessageBoxTitle)
                Me.txtFuelCost.Focus()
                Me.txtFuelCost.Clear()
            End If
        Catch ex As FormatException
            MessageBox.Show(strValidityCheckErrorMessage, _
                                strMessageBoxTitle)
            Me.txtFuelCost.Focus()
            Me.txtFuelCost.Clear()
        Catch ex As OverflowException
            MessageBox.Show(strValidityCheckErrorMessage, _
                                strMessageBoxTitle)
            Me.txtFuelCost.Focus()
            Me.txtFuelCost.Clear()
        Catch ex As SystemException
            MessageBox.Show(strValidityCheckErrorMessage, _
                            strMessageBoxTitle)
            Me.txtFuelCost.Focus()
            Me.txtFuelCost.Clear()
        End Try

        Return blnValidityCheck
    End Function

End Class
 
Back
Top