Collection members lost?

Collection members lost?

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


bgilbert posted on Tuesday, December 19, 2006

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

Copyright (c) Marimer LLC