Collection members lost. Please help.

Collection members lost. Please help.

Old forum URL: forums.lhotka.net/forums/t/2033.aspx


bgilbert posted on Wednesday, December 27, 2006

Let me try this again:

I have a class based on the ReadOnlyListBase. It will be stored in a shared variable in another business class. It has a public method that looks like this:

        If Me.Items.Count = 0 Then  ' Because this will be shared, I only fill the collection the first time.
                DataPortal.Fetch(Of ObjectRoles)(New Criteria(filter))
        End If
        For Each obj As ObjectRole In Me    ' *****Problem here*********
                '  Do stuff
        Next

In the DataPortal_Fetch method, I use typical code:
        RaiseListChangedEvents = False
        IsReadOnly = False
        Using cn As New SqlConnection(Database.MyConnection)
            cn.Open()
            Using cm As SqlCommand = cn.CreateCommand
                With cm
                    cm.CommandType = CommandType.StoredProcedure
                    cm.CommandText = "ObjectRolesSelect"
                    cm.Parameters.AddWithValue("@prmObjectName", criteria.ObjectName)
                    Using dr As New SafeDataReader(.ExecuteReader)
                        While dr.Read
                              '            Fill a new ObjectRole object with data reader row.
                              '            ObjectRole class inherits from ReadOnlyBase.
                            Me.Add(ObjectRole.GetObjectRole(dr))  
                            Debug.Print(Me.Count)  ' Correctly increments for each row.
                        End While
                    End Using
                    IsReadOnly = True
                    RaiseListChangedEvents = True
                End With
            End Using
        End Using

The problem (see **problem here** above) is that Me contains no children at this point. In the DataPortal_Fetch, the Add method correctly adds new objects and the Me.Count reflects this. Back at the For Each loop, Me.Count returns 0.

Any ideas would be helpful.

Barry

ajj3085 replied on Wednesday, December 27, 2006

Your problem is how you're loading the list.  DataPortal.Fetch will return a collection containing the objects, it will not fill the current instance.

Check out the PTracker demo that comes with Csla to see how to provide a cached list.

bgilbert replied on Wednesday, December 27, 2006

Great. That was it. Thanks for your help.

Copyright (c) Marimer LLC