Question adding a class to a collection

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:
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
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?
 
You've got 12 copies of the same object. You need to instantiate a new clsMortgageInfo object in each iteration of your For loop.

I've created a simple example where I'm using the same object each time through a loop and adding that same object to the loop multiple times and then creating a new object with each iteration of the loop. Hopefully it will help you correct your issue.

VB.NET:
Public Class TheClass


	Private _myInteger As Integer
	Public Property myInteger() As Integer
		Get
			Return _myInteger
		End Get
		Set(ByVal value As Integer)
			_myInteger = value
		End Set
	End Property

End Class

VB.NET:
Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles MyBase.Load

		Dim seed() As Integer = {1, 2, 3, 4, 5}

		Dim sameObjectCollection As New List(Of TheClass)
		Dim newObjectCollection As New List(Of TheClass)

		Dim sameObject As New TheClass
		For i As Integer = 0 To seed.Length - 1
			sameObject.myInteger = seed(i)
			sameObjectCollection.Add(sameObject)
		Next

		For i As Integer = 0 To seed.Length - 1
			Dim newObject As New TheClass
			newObject.myInteger = seed(i)
			newObjectCollection.Add(newObject)
		Next

	End Sub

End Class
 
Thank you!

Thank you very much. With your suggestion, and a little bit of other work, I got it to work right. Once again, thank you for taking the time to answer my question. I had been hesitant to post my question because I knew it was something stupid and I didn't want to seem like an idiot.
 
Not a problem at all. You showed prior effort and provided code to work with. If you keep that trend up you should have no problem getting suggestions from this forum.

Best of luck!
 
I am not the type of person to just give up easily or without ever trying. I worked for two weeks on figuring out how to go from showing a collection of a structure in a combo box to showing a collection of a class in a datagridview. Now that I have that ordeal out of the way, I will see if I can't get this program finished up.
 
Back
Top