我刚接触编码,只是将我的专业从EE转为IT - 在Visual Basic Functions方面遇到了麻烦。我认为我的错误是在变量声明函数的某处,但我并不完全确定。该程序应该将在医院花费的天数乘以我声明的常数= 350,并将所有杂项费用添加到该数字,但我返回0.任何人都可以帮我发现错误吗?
Visual Basic代码:
Const decStay_Rate As Decimal = 350
Private decLength As Integer
Private decMedication As Decimal
Private decSurgical As Decimal
Private decLab As Decimal
Private decPhysical As Decimal
Private decTotalStayPrice As Decimal
Private decTotalMiscCharges As Decimal
Private decTotal As Decimal
Dim decStay As Decimal
Function validateInputField() As Boolean
If Not Decimal.TryParse(txtLength.Text, decLength) Then
MessageBox.Show("Stay Length must be numeric")
End If
If Not Decimal.TryParse(txtMedication.Text, decMedication) Then
MessageBox.Show("Medication cost must be numeric")
End If
If Not Decimal.TryParse(txtSurgical.Text, decSurgical) Then
MessageBox.Show("Surgical cost must be numeric")
End If
If Not Decimal.TryParse(txtLabFees.Text, decLab) Then
MessageBox.Show("Lab fees must be numeric")
End If
If Not Decimal.TryParse(txtPhysicalRehab.Text, decPhysical) Then
MessageBox.Show("Physical Rehab cost must be numeric")
End If
Return True
End Function
Function CalcStayCharges(ByVal decLength As Decimal) As Decimal
Dim decTotalStayPrice As Decimal
decTotalStayPrice = decLength * decStay_Rate
Return decTotalStayPrice
End Function
Function CalcMiscCharges(ByVal decmedication As Decimal, ByVal decsurgical As Decimal, ByVal decLab As Decimal, ByVal decPhysical As Decimal) As Decimal
Dim decTotalMiscCharges As Decimal
decTotalMiscCharges = decmedication + decsurgical + decLab + decPhysical
Return decTotalMiscCharges
End Function
Private Function CalcTotalCharges(ByVal decTotalStayPrice As Decimal, ByVal decTotalMiscCharges As Decimal) As Decimal
Dim decTotalCharge As Decimal
decTotalCharge = decTotalStayPrice + decTotalMiscCharges
Return decTotalCharge
End Function
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtLabFees.Text = String.Empty
txtLength.Text = String.Empty
txtMedication.Text = String.Empty
txtPhysicalRehab.Text = String.Empty
txtSurgical.Text = String.Empty
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim decTotal As Decimal
lblOutput.Text = String.Empty
If validateInputField() Then
decTotal = CalcTotalCharges(decTotalStayPrice, decTotalMiscCharges)
lblOutput.Text = decTotal.ToString("c")
End If
End Sub
谢谢, 埃里克
答案 0 :(得分:0)
首先,您不需要Decimal.TryParse(txtLength.Text, decLength)
来检查它是否为数字。您可以使用IsNumeric IsNumeric(txtLength.Text)
。如果成功,Decimal.TryParse
会将txtLength.Text
的值分配给decLength
,但您不需要在您的情况下执行此操作。
我会将方法validateInputField
更改为:
Function validateInputField() As Boolean
If Not IsNumeric(txtLength.Text) Then
MessageBox.Show("Stay Length must be numeric")
Return False
End If
If Not IsNumeric(txtMedication.Text) Then
MessageBox.Show("Medication cost must be numeric")
Return False
End If
If Not IsNumeric(txtSurgical.Text) Then
MessageBox.Show("Surgical cost must be numeric")
Return False
End If
If Not IsNumeric(txtLabFees.Text) Then
MessageBox.Show("Lab fees must be numeric")
Return False
End If
If Not IsNumeric(txtPhysicalRehab.Text) Then
MessageBox.Show("Physical Rehab cost must be numeric")
Return False
End If
Return True
End Function
我删除了所有这些,因为它们会让你感到困惑:
Private decLength As Integer
Private decMedication As Decimal
Private decSurgical As Decimal
Private decLab As Decimal
Private decPhysical As Decimal
Private decTotalStayPrice As Decimal
Private decTotalMiscCharges As Decimal
Private decTotal As Decimal
Dim decStay As Decimal
然后,您需要将正确的值传递给方法CalcTotalCharges
:
decTotal = CalcTotalCharges(value1, value2)
以迂回的方式,你可能会追求:
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim decTotal As Decimal
lblOutput.Text = ""
If validateInputField() Then
decTotal = CalcTotalCharges(CalcStayCharges(CDec(txtLength.Text)), CalcMiscCharges(CDec(txtMedication.Text), CDec(txtSurgical.Text), CDec(txtLabFees.Text), CDec(txtPhysicalRehab.Text)))
lblOutput.Text = decTotal.ToString("c")
End If
End Sub