Collections.Generic question

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
VB.NET:
Imports System.Collections.Generic

Partial Class _Default
    Inherits System.Web.UI.Page

    Dim collection As New List(Of BlockRecord)
    Dim entry As New BlockRecord

    Protected Sub btnAddRecord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRecord.Click
        With entry
            .AcctNum = txtAcctNum.Text
            .EntryDate = txtDate.Text
            .DataSource = txtDataSource.Text
            .AsOfDate = txtAsOfDate.Text
            .Description = txtDescription.Text
            .Description2 = txtDescription2.Text
            .FileNameDesc = txtFileNameDesc.Text
            .Amount = txtAmount.Text
            .DebitCreditInd = txtDebitCreditInd.Text
        End With
        collection.Add(entry)

        Me.GridView1.DataSource = collection
        Me.GridView1.DataBind()
    End Sub

End Class

Public Class BlockRecord

    Private _AcctNum As Double
    Private _EntryDate As Date
    Private _DataSource As String
    Private _AsOfDate As Date
    Private _Description As String
    Private _Description2 As String
    Private _FileNameDesc As String
    Private _Amount As Double
    Private _DebitCreditInd As Double

    Public Property AcctNum() As Double
        Get
            Return _AcctNum
        End Get
        Set(ByVal value As Double)
            _AcctNum = value
        End Set
    End Property

    Public Property EntryDate() As Date
        Get
            Return _EntryDate
        End Get
        Set(ByVal value As Date)
            _EntryDate = value
        End Set
    End Property

    Public Property DataSource() As String
        Get
            Return _DataSource
        End Get
        Set(ByVal value As String)
            _DataSource = value
        End Set
    End Property

    Public Property AsOfDate() As Date
        Get
            Return _AsOfDate
        End Get
        Set(ByVal value As Date)
            _AsOfDate = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            _Description = value
        End Set
    End Property

    Public Property Description2() As String
        Get
            Return _Description2
        End Get
        Set(ByVal value As String)
            _Description2 = value
        End Set
    End Property

    Public Property FileNameDesc() As String
        Get
            Return _FileNameDesc
        End Get
        Set(ByVal value As String)
            _FileNameDesc = value
        End Set
    End Property

    Public Property Amount() As Double
        Get
            Return _Amount
        End Get
        Set(ByVal value As Double)
            _Amount = value
        End Set
    End Property

    Public Property DebitCreditInd() As Double
        Get
            Return _DebitCreditInd
        End Get
        Set(ByVal value As Double)
            _DebitCreditInd = value
        End Set
    End Property

End Class

Code works fine on the 1st itteration. When I attempt to add the second record rather than adding an additional entry to the list it overwrites it.

If someone could take a look at this and point out my error(s) it would be appreciated.

Thanks in advance.
 
When I attempt to add the second record rather than adding an additional entry to the list it overwrites it.
You're changing the original instance and adding it to the collection again, which means you're keeping a collection of references to the same object. No matter which item you're getting from this will return the same item. To fix it create a new object to add to the collection each time.

Ohh, shoot, I just noticed this is a web project, you have to learn about state management in ASP.NET. You also have to ask ASP.NET questions in ASP.NET section. I will move this one to ASP.NET section.
 
Last edited:
Oops, didn't realize I was on the wrong Data Access board when I created the thread.

Here's the solution I ended up using:

VB.NET:
Imports System.Collections.Generic

<Serializable()> _
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub btnAddRecord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRecord.Click
        Dim collection As List(Of BlockRecord)
        Dim entry As BlockRecord = New BlockRecord()

        If Session("Collection") Is Nothing Then
            collection = New List(Of BlockRecord)
        Else
            collection = DirectCast(Session("Collection"), List(Of BlockRecord))
        End If

        With entry
            .AcctNum = txtAcctNum.Text
            .EntryDate = txtDate.Text
            .DataSource = txtDataSource.Text
            .AsOfDate = txtAsOfDate.Text
            .Description = txtDescription.Text
            .Description2 = txtDescription2.Text
            .FileNameDesc = txtFileNameDesc.Text
            .Amount = txtAmount.Text
            .DebitCreditInd = txtDebitCreditInd.Text
        End With

        collection.Add(entry)

        Session("Collection") = collection

        Me.GridView1.DataSource = collection
        Me.GridView1.DataBind()
    End Sub
End Class
 

Latest posts

Back
Top