2016-11-16 51 views
1

我是編程新手,剛剛將我的專業從EE轉換到IT - 在使用Visual Basic函數時遇到問題。我認爲我的錯誤是在函數聲明的某個地方,但我不完全確定。該程序應該使醫院所花的時間乘以我聲明的常數= 350,並將所有雜項費用加到該數字中,但是我返回0.任何人都可以幫助我發現錯誤嗎?Visual Basic函數的變量聲明中的錯誤

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

歡迎埃裏克。正如你在vba標籤描述中看到的那樣,VBA和VB.NET不是等價的。 –

+0

謝謝,我糾正了這個問題 –

+0

在代碼中放置一個斷點(點擊左邊的空白處,得到一個紅色的點)然後繼續。您可以將鼠標指針懸停在變量上以獲取其當前值。 – peterG

回答

0

首先你不需要Decimal.TryParse(txtLength.Text, decLength)來檢查它是否是數字。你可以使用IsNumeric,所以IsNumeric(txtLength.Text)。如果成功,Decimal.TryParse會將值從txtLength.Text分配到decLength,但您不需要在您的情況下執行此操作。因爲它們給你造成混亂

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 

我刪除了所有這些:

我會改變方法validateInputField

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 
+1

修好了,謝謝。錯誤是在我忘記布爾函數和我的代碼在按鈕 –

+0

返回false沒有問題。很高興你對它進行了排序。 – Bugs