Vb.net Visual Basic函数的变量声明中出错
我对编码还不熟悉,刚把专业从EE转到IT——在Visual Basic函数方面遇到了问题。我认为我的错误在函数的变量声明中,但我不能完全确定。该程序应该将住院天数乘以我声明的常数=350,并将所有杂项费用加到该数字上,但我返回0。有人能帮我找出错误吗 Visual Basic代码:Vb.net Visual Basic函数的变量声明中出错,vb.net,function,variables,return,Vb.net,Function,Variables,Return,我对编码还不熟悉,刚把专业从EE转到IT——在Visual Basic函数方面遇到了问题。我认为我的错误在函数的变量声明中,但我不能完全确定。该程序应该将住院天数乘以我声明的常数=350,并将所有杂项费用加到该数字上,但我返回0。有人能帮我找出错误吗 Visual Basic代码: Const decStay_Rate As Decimal = 350 Private decLength As Integer Private decMedication As Decimal
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
谢谢,
Eric首先,您不需要
Decimal.TryParse(txtLength.Text,decLength)
来检查它是否是数字。您可以使用soIsNumeric(txtLength.Text)
<如果成功,code>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
首先,您不需要
Decimal.TryParse(txtLength.Text,decLength)
来检查它是否是数字。您可以使用soIsNumeric(txtLength.Text)
<如果成功,code>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
欢迎埃里克。正如您在vba标记描述中所看到的,vba和VB.NET是不等价的。谢谢,我纠正了这一点,在代码中添加了一个断点(单击左边空白处,得到一个红色点),然后逐步执行。您可以将鼠标指针悬停在变量上以获取其当前值。答案很好,我正在尝试。我只是想有一个方法来检查我的错误,以及在未来。为什么会被否决?好吧,我在顶部声明了变量=0,并认为我是从方法中的变量值中提取的,但实际上是从0值中提取的。我需要复习一下范围和变量声明。非常感谢您的帮助,并指出我的弱点所在。我将努力使我的变量声明不那么草率。保重,欢迎埃里克。正如您在vba标记描述中所看到的,vba和VB.NET是不等价的。谢谢,我纠正了这一点,在代码中添加了一个断点(单击左边空白处,得到一个红色点),然后逐步执行。您可以将鼠标指针悬停在变量上以获取其当前值。答案很好,我正在尝试。我只是想有一个方法来检查我的错误,以及在未来。为什么会被否决?好吧,我在顶部声明了变量=0,并认为我是从方法中的变量值中提取的,但实际上是从0值中提取的。我需要复习一下范围和变量声明。非常感谢您的帮助,并指出我的弱点所在。我将努力使我的变量声明不那么草率。小心点,Jinx修好了,谢谢。错误在于我忘记在布尔函数上返回false,而按钮上的代码没有问题。很高兴你把它整理好了。这就解决了它,谢谢。错误在于我忘记在布尔函数上返回false,而按钮上的代码没有问题。很高兴你把它整理好了。