【发布时间】:2015-04-24 12:52:56
【问题描述】:
粗体部分是我遇到问题的地方。至少这是我认为的问题。
我收到一条错误消息:
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
但我不知道为什么,有人可以帮助我吗?该应用程序在输入内容时工作,但不是。我迷失了为什么,我想我一直在看这段代码太久了。
代码和表格附在这里:
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
【问题讨论】:
-
在强制转换 (CDec) 之前,我总是会检查 TextBox 是否包含某些内容以及它是否是一个数字,否则你会得到一个你没有捕捉到的 execption
-
在引发异常时了解
txtLabor.Text是很有用的。 -
我不想问,但我对这种类型的编程是相对论的新手,而且我一直在拼凑这些......你能给我举个例子吗?
-
txtLabor 是“劳动力”的手动输入...我相信这就是您要问的。
-
Decimal.TryParse 文档中的示例。