Devin Devin - 1 month ago 13
Vb.net Question

VB Mortgage Calculator not displaying correct output

My professor is requiring us to create a mortgage calculator using sub procedures. I was able to complete this task all in one button click function without using sub's or ByVal / ByRef. However when I split it up into several different functions and subs I have 0 errors however when I go to click the button and calculate, the 3 display outputs are all 0. I'm assuming it is something in my Function calc block or in my btnCalculate_Click Sub.

Public Class frmMortgage

Dim annualRateOfInterest, monthlyPayment, begBalance As Double 'Declaring variables
Dim intForMonth, redOfPrincipal, endBalance As Double

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

Call getInput(annualRateOfInterest, monthlyPayment, begBalance)

Call displayOutput(intForMonth, redOfPrincipal, endBalance)

End Sub

Sub getInput(ByVal annualRateOfInterests As Double, ByVal monthlyPayment As Double, ByVal begBalance As Double) 'Values input into txt boxes
annualRateOfInterest = CDbl(txtInterest.Text) / 100 'Change number entered into a percentage
monthlyPayment = CDbl(txtPayment.Text)
begBalance = CDbl(txtBalance.Text)
End Sub

Function calc(ByVal annualRateOfInterest As Double, ByVal monthlyPayment As Double, ByVal begBalanceintForMonth As Double, ByVal redOfPrincipal As Double, ByVal endBalance As Double)
Return intForMonth = (annualRateOfInterest / 12) * begBalance 'Take percentage divided by 12 times beginning balance
Return redOfPrincipal = monthlyPayment - intForMonth
Return endBalance = begBalance - redOfPrincipal
End Function

Sub displayOutput(ByRef intForMonth As Double, ByRef redOfPrincipal As Double, ByRef endBalance As Double)
mtbMonth.Text = CDbl(intForMonth) 'Display the calculated data into correct textboxes.
mtbROP.Text = CDbl(redOfPrincipal)
mtbEndBalance.Text = CDbl(endBalance)
End Sub

End Class

Answer

I just merged it all in one Sub.

Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculate.Click
    ComputeMortgage(Val(txtInterest.Text), Val(txtPayment.Text), Val(txtBalance.Text))
End Sub

Sub ComputeMortgage(ByVal Interest As Double, ByVal monthlyPayment As Double, ByVal begBalance As Double)
    annualRateOfInterest = Interest / 100

    'COMPUTATION
    intForMonth = (annualRateOfInterest / 12) * begBalance 'Take percentage divided by 12 times beginning balance
    redOfPrincipal = monthlyPayment - intForMonth
    endBalance = begBalance - redOfPrincipal

    'DISPLAYING
    mtbMonth.Text = intForMonth 'Display the calculated data into correct textboxes.
    mtbROP.Text = redOfPrincipal
    mtbEndBalance.Text = endBalance

End Sub