Ini untuk tugas pekerjaan rumah untuk membuat aplikasi yang menghitung tarif sewa rumah liburan. Semua perhitungan harus dilakukan melalui fungsi. Setiap kali saya menjalankan program, semua output mengembalikan nol ($0,00). Saya dapat mengatakan bahwa ini karena "0" diteruskan ke fungsi alih-alih nilai yang diambil dari input pengguna. Karena fungsi validasi dapat mengambil input itu dengan baik, saya bingung apa masalahnya.

Saya telah mencoba merestrukturisasi fungsi saya dari FUNCTION_NAME(PARAM1, PARAM2...) menjadi VARIABLE = FUNCTION_NAME(PARAM1, PARAM2...), dan itu tidak berhasil.

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

        'Declare variables and constants
        Dim strFirstName As String
        Dim strLastName As String
        Dim strPhoneNumber As String
        Dim strEmailAddress As String
        Dim intDaysToRent As Integer
        Dim strSeason As String
        Dim strBuyerState As String
        Dim dblSubTotal As Double
        Dim dblTaxAmount As Double
        Dim dblFinalTotal As Double

        Const dblOffSeason As Double = 50
        Const dblPeakSeason As Double = 150
        Const dblStandardSeason As Double = 100
        Const dbl14DayDiscount As Double = 0.95
        Const dbl30DayDiscount As Double = 0.9
        Const dblTaxRate As Double = 0.1

        'Validate inputs
        If ValidateInputs(strFirstName, strLastName, strPhoneNumber, strEmailAddress, intDaysToRent, strSeason, strBuyerState) = True Then

            'Calculate subtotal
            dblSubTotal = CalculateSubTotal(intDaysToRent, strSeason, dbl14DayDiscount, dbl30DayDiscount, dblSubTotal)

            'Calculate tax amount
            dblTaxAmount = CalculateTaxAmount(dblSubTotal, strBuyerState, dblTaxRate, dblTaxAmount)


            'Calculate final total
            dblFinalTotal = CalculateFinalTotal(dblSubTotal, dblTaxAmount, dblFinalTotal)
____________________________________________________________

 Function ValidateInputs(ByVal FirstName As String, ByVal LastName As String, ByVal PhoneNumber As String, ByVal EmailAddress As String, ByVal DaysToRent As Integer, ByVal Season As String, ByVal BuyerState As String)

        'Main validation procedure
        If ValidateFirstName(FirstName) = True Then
            If ValidateLastName(LastName) = True Then
                If ValidatePhoneNumber(PhoneNumber) = True Then
                    If ValidateEmailAddress(EmailAddress) = True Then
                        If ValidateDaysToRent(DaysToRent) = True Then
                            If ValidateSeason(Season) = True Then
                                If ValidateBuyerState(BuyerState) = True Then
                                    Return True
                                Else Return False
                                End If
                            Else Return False
                            End If
                        Else Return False
                        End If
                    Else Return False
                    End If
                Else Return False
                End If
            Else Return False
            End If
        Else
            Return False
            Exit Function
        End If

    End Function
____________________________________________________________
    'First name validation subroutine
    Function ValidateFirstName(ByRef FirstName As String) As Boolean

        'Check that input exists
        If txtFirstName.Text Is String.Empty Then
            txtFirstName.BackColor = Color.Yellow
            txtFirstName.Focus()
            MessageBox.Show("Please enter the renter's first name.")
            Return False
            Exit Function

            'Check that input is a valid string
        ElseIf IsNumeric(txtFirstName.Text) Then
            txtFirstName.BackColor = Color.Yellow
            txtFirstName.Focus()
            MessageBox.Show("Please enter letters only.")
            Return False
            Exit Function
        Else
            FirstName = txtFirstName.Text
            Return True
        End If

    End Function
____________________________________________________________
    'Last name validation subroutine
    Function ValidateLastName(ByRef LastName As String) As Boolean

        'Check that input exists
        If txtLastName.Text Is String.Empty Then
            txtLastName.BackColor = Color.Yellow
            txtLastName.Focus()
            MessageBox.Show("Please enter the renter's last name.")
            Return False
            Exit Function

            'Check that input is valid string
        ElseIf IsNumeric(txtLastName.Text) Then
            txtLastName.BackColor = Color.Yellow
            txtLastName.Focus()
            MessageBox.Show("Please enter letters only.")
            Return False
            Exit Function
        Else
            LastName = txtLastName.Text
            Return True
        End If

    End Function
____________________________________________________________
    'Validate phone number subroutine
    Function ValidatePhoneNumber(ByRef PhoneNumber As String) As Boolean

        'Check that input exists
        If txtPhoneNumber.Text Is String.Empty Then
            txtPhoneNumber.BackColor = Color.Yellow
            txtPhoneNumber.Focus()
            MessageBox.Show("Please enter the renter's phone number.")
            Return False
            Exit Function

            'Check that input is valid string
        ElseIf IsNumeric(txtPhoneNumber.Text) Then
            PhoneNumber = txtPhoneNumber.Text
            Return True
        Else
            txtPhoneNumber.BackColor = Color.Yellow
            txtPhoneNumber.Focus()
            MessageBox.Show("Please enter numbers only.")
            Return False
            Exit Function
        End If

    End Function
____________________________________________________________
    'Validate email address subroutine
    Function ValidateEmailAddress(ByRef EmailAddress As String) As Boolean

        'Check that input exists
        If txtEmail.Text Is String.Empty Then
            txtEmail.BackColor = Color.Yellow
            txtEmail.Focus()
            MessageBox.Show("Please enter the renter's email address.")
            Return False
            Exit Function
        Else
            EmailAddress = txtEmail.Text
            Return True
        End If

    End Function
____________________________________________________________
    'Validate days to rent subroutine
    Function ValidateDaysToRent(ByVal DaysToRent As String) As Boolean

        'Check that input exists
        If txtDaysRented.Text Is String.Empty Then
            txtDaysRented.BackColor = Color.Yellow
            txtDaysRented.Focus()
            MessageBox.Show("Please enter the number of days the condo will be rented.")
            Return False

            'Check that input is valid string
        ElseIf IsNumeric(txtDaysRented.Text) Then
            DaysToRent = txtDaysRented.Text
            Return True
        Else
            txtDaysRented.BackColor = Color.Yellow
            txtDaysRented.Focus()
            MessageBox.Show("Please enter numbers only.")
            Return False
            Exit Function
        End If

    End Function
____________________________________________________________
    'Season validation subroutine
    Function ValidateSeason(ByVal Season As String) As Boolean

        'Check that input exists
        If cboSeason.Text Is String.Empty Then
            cboSeason.BackColor = Color.Yellow
            cboSeason.Focus()
            MessageBox.Show("Please enter the season the condo will be rented in.")
            Return False
            Exit Function
        Else
            Season = cboSeason.Text
            Return True
        End If

    End Function
____________________________________________________________
    'State validation subroutine
    Function ValidateBuyerState(ByVal BuyerState As String) As Boolean

        'Check that input exists
        If cboState.Text Is String.Empty Then
            cboState.BackColor = Color.Yellow
            cboState.Focus()
            MessageBox.Show("Please enter the season the renter's state of residence.")
            Return False
            Exit Function
        Else
            BuyerState = cboState.Text
            Return True
        End If

    End Function
____________________________________________________________
  'Subtotal Calcultion Function
    Function CalculateSubTotal(ByVal DaysToRent As Integer, ByRef Season As String, ByVal Discount14 As Double, ByVal Discount30 As Double, ByRef Subtotal As Double) As Double

        'Declare variable
        Dim dblSubTotal As Double
        Dim dblSeasonRate As Double
        Dim strSeason As String

        cboSeason.Text = strSeason

        'Determine season rate
        If Season = "Off Season" Then
            dblSeasonRate = 50
        ElseIf Season = "Peak Season" Then
            dblSeasonRate = 150
        ElseIf Season = "Standard Season" Then
            dblSeasonRate = 100
        End If

        'Calculate subtotal
        dblSubTotal = DaysToRent * dblSeasonRate

        'Calculate discount (if applicable)
        If DaysToRent > 14 And DaysToRent < 31 Then
            dblSubTotal = Subtotal * Discount14
        ElseIf DaysToRent > 30 Then
            dblSubTotal = Subtotal * Discount30
        End If

        Return dblSubTotal

    End Function
____________________________________________________________

    'Tax amount calculation function
    Function CalculateTaxAmount(ByVal Subtotal As Double, ByVal State As String, ByVal TaxRate As Double, ByRef TaxAmount As Double) As Double

        'Calculate tax rate
        If State = "Florida" Then
            TaxAmount = 0
        Else
            TaxAmount = Subtotal * TaxRate
        End If

        Return TaxAmount

    End Function
____________________________________________________________

    'Final total calculation function
    Function CalculateFinalTotal(ByVal Subtotal As Double, ByVal TaxAmount As Double, ByRef FinalTotal As Double) As Double

        'Calculate final total
        FinalTotal = Subtotal + TaxAmount

        Return FinalTotal

    End Function

0
Ethan 3 April 2019, 06:00

1 menjawab

Jawaban Terbaik

Anda meneruskan parameter ke ValidateInputs menggunakan byval. Ini membuat salinan variabel-variabel ini dalam fungsi & salinan itulah yang diberikan oleh fungsi validasi individu. Variabel dalam btnSubmit_Click dibiarkan sebagai nilai defaultnya & inilah yang diteruskan ke fungsi perhitungan.

Mengubah parameter untuk menggunakan byref akan menghentikan ini:

Function ValidateInputs(ByRef FirstName As String, ByRef LastName As String, ByRef PhoneNumber As String, ByRef EmailAddress As String, ByRef DaysToRent As Integer, ByRef Season As String, ByRef BuyerState As String)
1
ItsPete 3 April 2019, 04:30