【发布时间】:2017-03-30 20:58:48
【问题描述】:
我是编码新手,刚刚将我的专业从 EE 转到 IT - 在 Visual Basic 函数方面遇到了问题。我认为我的错误是在函数上声明变量的某个地方,但我不完全确定。该程序应该将在医院度过的天数乘以我声明的常数 = 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
谢谢, 埃里克
【问题讨论】:
-
欢迎埃里克。正如您在 vba 标记描述中看到的那样,VBA 和 VB.NET 并不等同。
-
谢谢,我更正了
-
在代码中放一个断点(点击左边空白处,得到一个红点)然后单步执行。您可以将鼠标指针悬停在变量上以获取它们的当前值。
-
答案很好,我试试看。我只是想要一种方法来检查我将来的错误。为什么会被否决?
-
好的,所以我在顶部声明了变量 = 0,并认为我是从方法中的变量值中提取的,但实际上是在提取 0 值。我需要复习范围和变量声明。非常感谢您的帮助以及指出我的弱点在哪里。我将努力使我的变量声明不那么草率。小心金克丝
标签: vb.net function variables return