I have a form to obtain financial information from the user including a loan amount, rate, and term. I'm supposed to call a class to do the work and then return the answer to the form. However, for some reason, whenever my program moves out of the class, back to the form, then back to the class, the variables I have the class get reset to 0. My code is below. Any help would be awesome.
This is the code from my form:
'Preform calculation when button is clicked
Private Sub buttonCalc_Click(sender As Object, e As EventArgs) Handles buttonCalc.Click
Dim loanAmount As New finClass
Dim rate As New finClass
Dim term As New finClass
Dim payment As New finClass
loanAmount.getLoanAmount = textBoxMortgageAmount.Text
rate.getAnnualRate = comboBoxAnnualRate.Text.Replace("%", "")
term.getTermInYears = comboBoxTerm.SelectedItem
textBoxMonthlyPayment.Text = payment.setMonthlyPayment
End Sub
And this code from the associated class:
Public Class finClass
Private loanAmt As Decimal
Private termValue As Decimal
Private rateValue As Decimal
Public paymentValue As Decimal
Public WriteOnly Property getLoanAmount
Set(value)
If IsNumeric(value) AndAlso value > 1000 AndAlso value < 10000000 Then
Me.loanAmt = value
Else
MessageBox.Show("Mortgage amount must be a number between $1,000 and $10,000,000", "Invalid Number", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Set
End Property
Public WriteOnly Property getAnnualRate
Set(value)
rateValue = value.Replace("%", "")
If rateValue < 1 Then
rateValue *= 100
End If
End Set
End Property
Public WriteOnly Property getTermInYears
Set(value)
termValue = value
End Set
End Property
Public ReadOnly Property setMonthlyPayment
Get
Return -Pmt((rateValue / 1200), termValue * 12, Me.loanAmt)
End Get
End Property
End Class
I don't work in VS but it looks like you are creating four instances of the class. I'm pretty sure this is not correct.
Private Sub buttonCalc_Click(sender As Object, e As EventArgs) Handles buttonCalc.Click
Dim loanAmount As New finClass '## Creates NEW finClass object
Dim rate As New finClass '## Creates NEW finClass object
Dim term As New finClass '## Creates NEW finClass object
Dim payment As New finClass '## Creates NEW finClass object
loanAmount.getLoanAmount = textBoxMortgageAmount.Text
rate.getAnnualRate = comboBoxAnnualRate.Text.Replace("%", "")
term.getTermInYears = comboBoxTerm.SelectedItem
textBoxMonthlyPayment.Text = payment.setMonthlyPayment
End Sub
So each of those objects is a different instance of a finClass
object. So each time you "re-enter" the class, you're actually entering a different instace of that class object, which is why all the variables are value = 0. They have not been instantiated or set, yet.
Try this instead, to create one class object which you manipulate with the methods in your class module:
Private Sub buttonCalc_Click(sender As Object, e As EventArgs) Handles buttonCalc.Click
Dim objFinClass as New finClass
objFinClass.getLoanAmount = textBoxMortgageAmount.Text
objFinClass.getAnnualRate = comboBoxAnnualRate.Text.Replace("%", "")
objFinClass.getTermInYears = comboBoxTerm.SelectedItem
textBoxMonthlyPayment.Text = objFinClass.setMonthlyPayment
End Sub