2016-11-16 2 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 в случае успеха, однако вам не нужно это делать в вашем случае.

Я хотел бы изменить метод 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 
+1

Это исправлено, спасибо. Ошибка заключалась в том, что я забыл вернуть false в логическую функцию, а мой код на кнопке –

+0

Не проблема. Рад, что вы его отсортировали. – Bugs

Смежные вопросы