kennywayne07
New member
- Joined
- Dec 4, 2008
- Messages
- 4
- Programming Experience
- Beginner
I am having trouble getting a class I created to correctly populate a collection. If I use a structure instead of a class the collection is populated correctly.
My class looks like this:
The sub that I am using to populate the collection looks like this:
I cant figure out for the life of me how to make the collection have the correct data. If I use 12 as the number of payments, I end up with 12 items that all have the same info as the 12th item. When I use a structure with this I end up with 12 items that are all different...1,2,3...11,12. I am not sure if any of this makes sense. What do I need to do to change from a structure to a class and have the data come out the same?
My class looks like this:
VB.NET:
Public Class clsMortgageInfo
Public dblPaymentNumber As Double
Public Property paymentNumber() As Double
Get
Return dblPaymentNumber
End Get
Set(ByVal value As Double)
dblPaymentNumber = value
End Set
End Property
Private dblInterestPayment As Double
Public Property interestPayment() As Double
Get
Return dblInterestPayment
End Get
Set(ByVal value As Double)
dblInterestPayment = value
End Set
End Property
Private dblPrincipalPayment As Double
Public Property principalPayment() As Double
Get
Return dblPrincipalPayment
End Get
Set(ByVal value As Double)
dblPrincipalPayment = value
End Set
End Property
Public Property totalPrincipal() As Double
Get
Return dblTotalPrincipal
End Get
Set(ByVal value As Double)
dblTotalPrincipal = value
End Set
End Property
Private dblTotalPrincipal As Double
Public Property totalInterest() As Double
Get
Return dblTotalInterest
End Get
Set(ByVal value As Double)
dblTotalInterest = value
End Set
End Property
Private dblTotalInterest As Double
Public Property totalPayments() As Double
Get
Return dblTotalPayments
End Get
Set(ByVal value As Double)
dblTotalPayments = value
End Set
End Property
Private dblTotalPayments As Double
Public Property monthlyPayment() As Double
Get
Return dblMonthlyPayment
End Get
Set(ByVal value As Double)
dblMonthlyPayment = value
End Set
End Property
Private dblMonthlyPayment As Double
Public Property loanBalance() As Double
Get
Return dblLoanBalance
End Get
Set(ByVal value As Double)
dblLoanBalance = value
End Set
End Property
Private dblLoanBalance As Double
Public Property numberOfPeriodsNeeded() As Double
Get
Return dblNumberOfPeriodsNeeded
End Get
Set(ByVal value As Double)
dblNumberOfPeriodsNeeded = value
End Set
End Property
Private dblNumberOfPeriodsNeeded As Double
Public Property newPresentValue() As Double
Get
Return dblNewPresentValue
End Get
Set(ByVal value As Double)
dblNewPresentValue = value
End Set
End Property
Private dblNewPresentValue As Double
Public Property monthlyTaxes() As Double
Get
Return dblMonthlyTaxes
End Get
Set(ByVal value As Double)
dblMonthlyTaxes = value
End Set
End Property
Private dblMonthlyTaxes As Double
Public Property yearlyTaxes() As Double
Get
Return dblYearlyTaxes
End Get
Set(ByVal value As Double)
dblYearlyTaxes = value
End Set
End Property
Private dblYearlyTaxes As Double
Public Property monthlyHomeownersInsurance() As Double
Get
Return dblMonthlyHomeownersInsurance
End Get
Set(ByVal value As Double)
dblMonthlyHomeownersInsurance = value
End Set
End Property
Private dblMonthlyHomeownersInsurance As Double
Public Property yearlyHomeownersInsurance() As Double
Get
Return dblYearlyHomeownersInsurance
End Get
Set(ByVal value As Double)
dblYearlyHomeownersInsurance = value
End Set
End Property
Private dblYearlyHomeownersInsurance As Double
Public Property privateMortgageInsurance() As Double
Get
Return dblPrivateMortgageInsurance
End Get
Set(ByVal value As Double)
dblPrivateMortgageInsurance = value
End Set
End Property
Private dblPrivateMortgageInsurance As Double
Public Property dataChanged() As Boolean
Get
Return blnDataChanged
End Get
Set(ByVal value As Boolean)
blnDataChanged = value
End Set
End Property
Private blnDataChanged As Boolean
End Class
The sub that I am using to populate the collection looks like this:
VB.NET:
Public Sub CalculateMortgageData()
giveValueToVariables()
mortgageCollection = New clsMortgageInfoCollection(Of clsMortgageInfo)
Dim mortgage As New clsMortgageInfo
With mortgage
.dataChanged = False
.loanBalance = (dblLoanAmount - dblDownPayment)
dblPresentValue = -(.loanBalance)
dblMonthlyPayment = Pmt(dblMonthlyInterestRate, dblNumberOfPayments, dblPresentValue)
determineBalloonData()
If Not dblFutureValue = PAID_OFF Then
dblNewFutureValue = FV(dblMonthlyInterestRate, dblFutureValue, dblMonthlyPayment, dblPresentValue, 0)
End If
frmAmoritizationForm.txtbxPrincipalPayment.Text = FormatCurrency(dblMonthlyPayment.ToString)
.monthlyPayment = dblMonthlyPayment
Dim count As Integer = 0
Dim dblPaymentNumber As Integer
For dblPaymentNumber = 1 To dblNumberOfPayments
.paymentNumber = dblPaymentNumber
.newPresentValue = (dblPresentValue - (dblPresentValue + .loanBalance))
.numberOfPeriodsNeeded = NPer(dblMonthlyInterestRate, dblMonthlyPayment, .newPresentValue, dblNewFutureValue, 0)
If Not dblNewFutureValue > .loanBalance Then
If Not .numberOfPeriodsNeeded <= 1 Then
.interestPayment = IPmt(dblMonthlyInterestRate, 1, .numberOfPeriodsNeeded, .newPresentValue, dblNewFutureValue, 0)
.principalPayment = PPmt(dblMonthlyInterestRate, 1, .numberOfPeriodsNeeded, .newPresentValue, dblNewFutureValue, 0)
count = count + 1
.totalPrincipal = .totalPrincipal + .principalPayment
.totalInterest = .totalInterest + .interestPayment
.totalPayments = .totalPayments + (.principalPayment + .interestPayment)
.loanBalance = .loanBalance - .principalPayment
mortgageCollection.Add(mortgage)
Else
.interestPayment = .loanBalance * dblMonthlyInterestRate
.principalPayment = .loanBalance
count = count + 1
.totalPrincipal = .totalPrincipal + .principalPayment
.totalInterest = .totalInterest + .interestPayment
.totalPayments = .totalPayments + (.principalPayment + .interestPayment)
.loanBalance = .loanBalance - .principalPayment
mortgageCollection.Add(mortgage)
End If
End If
Next
frmAmoritizationForm.lstbxTotals.Items.Clear()
frmAmoritizationForm.lstbxTotals.Items.Add("The total interest that will be paid after " & count & " payments is " & FormatCurrency(.totalInterest) & ".")
frmAmoritizationForm.lstbxTotals.Items.Add("The total principal that will be paid after " & count & " payments is " & FormatCurrency(.totalPrincipal) & ".")
frmAmoritizationForm.lstbxTotals.Items.Add("The total amount that will be paid after " & count & " payments is " & FormatCurrency(.totalPayments) & ".")
End With
End Sub