Vb.net 类型为'的首次机会例外;系统。无效卡斯特例外';发生在Microsoft.VisualBasic.dll中
粗体部分是我遇到问题的地方。至少我认为这就是问题所在 我得到一个错误,说: Microsoft.VisualBasic.dll中出现类型为“System.InvalidCastException”的第一次意外异常 此处指出了错误:Vb.net 类型为'的首次机会例外;系统。无效卡斯特例外';发生在Microsoft.VisualBasic.dll中,vb.net,visual-studio-2012,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
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
但我不明白为什么,有人能帮我吗?当输入内容时,应用程序会工作,但不会。我迷路了为什么,我想我已经看这个代码太久了
代码和表格附于此处:
公共类表单1
将decLabor设置为“十进制”以保留工时
将零件标注为小数,以保持零件的价格
Dim DECILANDLUBE(十进制)用于保存机油和润滑油总成的总值
Dim DECRUSHES As Decimal'用于保存刷新组的总值
Dim decMisc As Decimal'用于保存杂项的总值。团体
以十进制表示的十进制总数
Dim decServicesLabor作为小数'用于保存劳动力和服务总额
十进制常数decOilCharge=25D
十进制常数decLube=18D
十进制常数衰减器=30D
十进制常数decTrans=80D
常量decInspect为十进制=15D
十进制常数=100D
常量十进制=20D
常数declarabrate(十进制=20D)
十进制常数decTax=0.06D
私有子BTNCalCalculate_Click(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理btnCalculate。单击
'此过程计算订单的总数。
Dim decTotal As Decimal'保存订单总数
decServicesLabor=OilLubeCharges()+冲洗费用()+杂项费用()+其他费用()
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”)
端接头
**函数PartsIsValid()为布尔值
'声明一个值以临时保存零件值。
以十进制表示的Dim DECTEMP值
如果不是Decimal.TryParse(txtParts.Text,decTempValue),则
Show(“输入零件成本的数值”)
返回错误
如果结束
如果DectTempValue<0,则
MessageBox.Show(“输入零件成本的正值”)
如果结束
'如果到目前为止,该值是有效的,那么返回true。
返回真值
端函数
函数LaborIsValid()为布尔值
'声明一个值以临时保留人工值。
Dim DECTEMPALUE2为十进制
如果不是Decimal.TryParse(txtrail.Text,decTempValue2),则
Show(“输入人工成本的数值”)
返回错误
如果结束
如果DectTempValue2小于0,则
Show(“输入人工成本的正值”)
如果结束
'如果到目前为止,该值是有效的,那么返回true。
返回真值
端函数**
Private Sub btnClear\u Click(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理btnClear。Click
'此过程将控件重置为默认值
重置润滑油润滑卡()
重置冲销费用()
重置杂项费用()
其他费用()
'清除“零件和人工”框中的文本框
txtParts.Clear()
txtLabor.Clear()
'清除摘要框中的框。
lblpart.Text=String.Empty
lblSerandLab.Text=String.Empty
lbltax.Text=String.Empty
lbltotalprice.Text=String.Empty
端接头
私有子btnExit_Click(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理btnExit。单击
'结束应用程序。
我
端接头
函数OilLubeCharges()为十进制
'此函数返回机油和润滑油费用的成本。
Dim decCostOfOilLube作为十进制=0
如果ChkOilChange.Checked=True,则
脱costofoillube+=脱油电荷
如果结束
如果chkLubeJob.Checked=True,则
脱钴润滑油+=脱钴润滑油
如果结束
回流式脱costofoillube
端函数
函数FlushCharges()为十进制
'此函数返回刷新费用的成本。
Dim decCostOfFlush作为十进制=0
如果chkradiator.Checked=True,则
decCostOfFlush+=decflush
如果结束
如果chktransm.Checked=True,则
decCostOfFlush+=decTrans
如果结束
返回去成本冲洗
端函数
函数MiscCharges()为十进制
'此函数返回杂项成本。
Dim DECCOSTOFMIC作为十进制=0
如果chkInsp.Checked=True,则
decCostOfMisc+=decInspect
如果结束
如果chkrpl消音器.Checked=True,则
decCostOfMisc+=decCostOfMisc
如果结束
如果chktirerotation.Checked=True,则
decCostOfMisc+=decTireRot
如果结束
退票
端函数
函数PartsCost()为十进制
'此函数返回零件的成本。
decParts=CDec(txtParts.Text)
退货零件
端函数
函数OtherCharges()为十进制
'此函数返回人工和零件的成本。
Dim DECLARBOR按十进制收费
decLarborCharge=CDec(txtLabor.Text)
退货申报费
端函数
函数CalcTax(ByVal decAmount作为十进制)作为十进制
'此函数接收零件数量。它根据零件金额计算并返回零件税。
申报十年*十年税
端函数
私人分包商
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
Dim decLarborCharge As Decimal
If IsNumeric(txtLabor.Text) Then
decLarborCharge = CDec(txtLabor.Text)
End If
Return decLarborCharge
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