Microsoft.VisualBasic.dll中出现“System.InvalidCastException”类型的第一次机会异常

时间:2015-04-24 12:52:56

标签: vb.net visual-studio-2012

大胆的部分是我遇到问题的地方。至少这是我认为的问题。

我收到错误说:

Microsoft.VisualBasic.dll中出现'System.InvalidCastException'类型的第一次机会异常

这里指出了错误:

Function OtherCharges() As Decimal
        ' This function returns the cost of labor and parts.
        Dim decLarborCharge As Decimal
        **decLarborCharge = CDec(txtLabor.Text)**
        Return decLarborCharge

    End Function

但我无法弄清楚为什么,有人可以帮助我吗?该应用程序在输入内容时有效,但不能。我迷失了为什么,我想我一直在看这段代码太久了。

代码和表格附在此处:

https://onedrive.live.com/redir?resid=14EB9A8B891F0758!7083&authkey=!AHv97T0LPXok5D8&ithint=file%2czip

    Public Class Form1
    Dim decLabor As Decimal             'To hold the hours in Labor
    Dim decParts As Decimal             'To hold the price of Parts
    Dim decOilandLube As Decimal        'To Hold the total value of the Oil & Lube Group
    Dim decFlushes As Decimal           'To hold the total value of the Flushes Group
    Dim decMisc As Decimal              'To Hold the total value of the Misc. Group
    Dim decTotal As Decimal
    Dim decServicesLabor As Decimal     'To hold the labor and services total
    Const decOilCharge As Decimal = 25D
    Const decLube As Decimal = 18D
    Const decRadiator As Decimal = 30D
    Const decTrans As Decimal = 80D
    Const decInspect As Decimal = 15D
    Const decMuffler As Decimal = 100D
    Const decTireRot As Decimal = 20D
    Const decLaborRate As Decimal = 20D
    Const decTax As Decimal = 0.06D



    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        ' This proedure calculates the total of an order.

        Dim decTotal As Decimal ' holds the order total

        decServicesLabor = OilLubeCharges() + FlushCharges() + MiscCharges() + OtherCharges()
        decParts = PartsCost()
        'decTotal = decServicesLabor + decTax
        decTotal = decServicesLabor + decTax + decParts

        lblSerandLab.Text = decServicesLabor.ToString("c")
        lblParts.Text = decParts.ToString("c")
        lblTax.Text = decTax.ToString("c")
        lbltotalprice.Text = decTotal.ToString("c")
    End Sub

    **Function PartsIsValid() As Boolean
        ' Declare a value to temporarily hold the parts value.
        Dim decTempValue As Decimal
        If Not Decimal.TryParse(txtParts.Text, decTempValue) Then
            MessageBox.Show("Enter a numeric value for the parts cost.")
            Return False
        End If
        If decTempValue < 0 Then
            MessageBox.Show("Enter a positive value for the parts cost.")
        End If
        ' If we have made it this far, the value is valid, so return true.
        Return True
    End Function
    Function LaborIsValid() As Boolean
        ' Declare a value to temporarily hold the labor value.
        Dim decTempValue2 As Decimal
        If Not Decimal.TryParse(txtLabor.Text, decTempValue2) Then
            MessageBox.Show("Enter a numeric value for the labor cost.")
            Return False
        End If
        If decTempValue2 < 0 Then
            MessageBox.Show("Enter a positive value for the labor cost.")
        End If
        ' If we have made it this far, the value is valid, so return true.
        Return True
    End Function**


    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
        ' This procedure resets the controls to default values

        ResetOilLubeCharges()
        ResetFlushCharges()
        ResetMiscCharges()
        OtherCharges()
        ' Clears the text Boxes in the Parts and Labor box
        txtParts.Clear()
        txtLabor.Clear()
        ' Clears the boxes in the Summary Box.
        lblparts.Text = String.Empty
        lblSerandLab.Text = String.Empty
        lbltax.Text = String.Empty
        lbltotalprice.Text = String.Empty
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
        ' End the application.
        Me.Close()
    End Sub
    Function OilLubeCharges() As Decimal
        'This function returns the cost of the Oil and Lube Charges.

        Dim decCostOfOilLube As Decimal = 0

        If ChkOilChange.Checked = True Then
            decCostOfOilLube += decOilCharge
        End If
        If chkLubeJob.Checked = True Then
            decCostOfOilLube += decLube
        End If
        Return decCostOfOilLube
    End Function
    Function FlushCharges() As Decimal
        ' This function returns the cost of the Flush Charges.

        Dim decCostOfFlush As Decimal = 0

        If chkradiator.Checked = True Then
            decCostOfFlush += decFlushes
        End If
        If chktransm.Checked = True Then
            decCostOfFlush += decTrans

        End If
        Return decCostOfFlush
    End Function
    Function MiscCharges() As Decimal
        ' This function returns the cost of misc.
        Dim decCostOfMisc As Decimal = 0

        If chkInsp.Checked = True Then
            decCostOfMisc += decInspect
        End If
        If chkrplmuffler.Checked = True Then
            decCostOfMisc += decMuffler
        End If
        If chktirerotation.Checked = True Then
            decCostOfMisc += decTireRot
        End If
        Return decCostOfMisc
    End Function

    Function PartsCost() As Decimal
        ' This function returns the cost of parts.
        decParts = CDec(txtParts.Text)
        Return decParts
    End Function


    Function OtherCharges() As Decimal
        ' This function returns the cost of labor and parts.
        Dim decLarborCharge As Decimal
        decLarborCharge = CDec(txtLabor.Text)
        Return decLarborCharge

    End Function
    Function CalcTax(ByVal decAmount As Decimal) As Decimal
        ' this function receives the parts amount. It calculates and returns the parts tax, based on the parts amount.
        Return decAmount * decTax
    End Function


    Private Sub ResetOilLubeCharges()
        ' This procedure resets the Oil and lube selection.
        ChkOilChange.Checked = False
        ChkLubeJob.Checked = False
    End Sub
    Sub ResetFlushCharges()
        ' This procedure resets flush charge selection.
        chkradiator.Checked = False
        chktransm.Checked = False
    End Sub
    Sub ResetMiscCharges()
        ' This procedure resets all misc charges.
        chkInsp.Checked = False
        chkrplmuffler.Checked = False
        chktirerotation.Checked = False
    End Sub

End Class

2 个答案:

答案 0 :(得分:2)

Dim decLarborCharge As Decimal

If IsNumeric(txtLabor.Text) Then
   decLarborCharge = CDec(txtLabor.Text)
End If

Return decLarborCharge

即。将内容转换为十进制类型,仅当它是字符串形式的数值时。

答案 1 :(得分:2)

尝试一些防御性编程:

Function OtherCharges() As Decimal 
        ' This function returns the cost of labor and parts.
        Dim decLarborCharge As Decimal= Decimal.Zero

        If Not String.IsNullOrWhiteSpace(txtLabor.Text) Then
            decLarborCharge = Decimal.TryParse(txtLabor.Text, decLarborCharge)
        End If

        Return decLarborCharge

    End Function

否则,简单地试一试,第一次机会不应该出现