VB.NET • Create a Collection of Person Objects

Listing 1. The PersonCollection class works as a container for Person objects. The databinding infrastructure looks at the type returned by the Item member and lets you bind other controls to any of its properties.

Imports System.ComponentModel

Public Class PersonCollection
   Inherits Component
   Implements IList

   ' protected property List
   Private m_List As New ArrayList

   ' public members

   Public Function Add(ByVal item As Person) _
      As Person
      Me.List.Add(item)
      Return item
   End Function

   Default Public Property Item(ByVal index As _
      Integer) As Person
      Get
         Return DirectCast(Me.List(index), Person)
      End Get
      Set(ByVal Value As Person)
         Me.List(index) = Value
      End Set
   End Property

   Public Sub Remove(ByVal item As Person)
      Me.List.Remove(item)
   End Sub

   ' protected members

   Protected ReadOnly Property List() As ArrayList
      Get
         Return m_List
      End Get
   End Property

   ' IList public members

   Sub CopyTo(ByVal array As Array, ByVal index _
      As Integer) Implements ICollection.CopyTo
      Me.List.CopyTo(array, index)
   End Sub

   Public ReadOnly Property Count() As Integer _
      Implements ICollection.Count
      Get
         Return Me.List.Count
      End Get
   End Property

   Public ReadOnly Property IsSynchronized() As _
      Boolean Implements ICollection.IsSynchronized
      Get
         Return Me.List.IsSynchronized
      End Get
   End Property

   Public ReadOnly Property SyncRoot() As Object _
      Implements ICollection.SyncRoot
      Get
         Return Me.List.SyncRoot
      End Get
   End Property

   Public Function GetEnumerator() As IEnumerator _
      Implements IEnumerable.GetEnumerator
      Return Me.List.GetEnumerator()
   End Function

   Public Sub Clear() Implements IList.Clear
      Me.List.Clear()
   End Sub

   Public ReadOnly Property IsFixedSize() _
      As Boolean Implements IList.IsFixedSize
      Get
         Return Me.List.IsFixedSize
      End Get
   End Property

   Public ReadOnly Property IsReadOnly() As Boolean _
      Implements IList.IsReadOnly
      Get
         Return Me.List.IsReadOnly
      End Get
   End Property

   Public Sub RemoveAt(ByVal index As Integer) _
      Implements IList.RemoveAt
      Me.List.RemoveAt(index)
   End Sub

   ' private members

   Private Function Add(ByVal value As Object) _
      As Integer Implements IList.Add
      Me.List.Add(value)
   End Function

   Private Function Contains(ByVal value As Object) _
      As Boolean Implements IList.Contains
      Return Me.List.Contains(value)
   End Function

   Private Function IndexOf(ByVal value As Object) _
      As Integer Implements IList.IndexOf
      Return Me.List.IndexOf(value)
   End Function

   Private Sub Insert(ByVal index As Integer, _
      ByVal value As Object) Implements IList.Insert
      Me.List.Insert(index, value)
   End Sub

   Private Property ListItem(ByVal index As _
      Integer) As Object Implements IList.Item
      Get
         If index >= 0 AndAlso index < _
            Me.List.Count Then
            Return Me.List(index)
         Else
            Return Nothing
         End If
      End Get
      Set(ByVal Value As Object)
         Me.List(index) = Value
      End Set
   End Property

   Private Sub Remove(ByVal value As Object) _
      Implements IList.Remove
      Me.List.Remove(value)
   End Sub
End Class