Subscribe

RSS Feed (xml)

ASP.NET Creating Custom Collection

A custom collection class allows the use of the


For Each syntax (foreach in C#) to


loop through a set of custom typed items.The custom collection class must internally


keep references to the items with an array or other type of collection (ArrayList,


HashTable).The .NET Framework then requires the following two interfaces to be


implemented in order to support the For Each syntax:




  • System.Collections.IEnumerable



  • Function GetEnumerator() As IEnumerator



  • System.Collections.IEnumerator



  • Sub Reset()



  • Function MoveNext() As Boolean



  • ReadOnly Property Current As Object



In <script runat="server" /> block or codebehind:


Sub Page_Load()


Dim _customCollection As CustomCollection


Dim _customItem As CustomItem


Dim _index As Integer


_customCollection = New CustomCollection(10)


For Each _customItem In _customCollection


_index = _customItem.Index


Next


End Sub


Custom Collection defined in CustomCollection.vb


Public Class CustomCollection


Implements IEnumerable, IEnumerator


Private customItems() As CustomItem


Private currentIndex As Integer = -1


Public Sub New(ByVal Count As Integer)


Dim index As Integer


ReDim customItems(Count - 1)


For index = 0 To Count - 1


customItems(index) = New CustomItem(index)


Next


End Sub


#Region "Implementation of IEnumerable"


Public Function GetEnumerator() As Ienumerator


Implements IEnumerable.GetEnumerator


Return CType(Me, IEnumerator)


End Function


#End Region


#Region "Implementation of IEnumerator"


Public Sub Reset() Implements IEnumerator.Reset


currentIndex = -1


End Sub


Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext


If currentIndex < customItems.Length - 1 Then


currentIndex = currentIndex + 1


Return True


Else


Return False


End If


End Function


Public ReadOnly Property Current() As Object


Implements IEnumerator.Current


Get


Return customItems(currentIndex)


End Get


End Property


#End Region


End Class


Custom Item defined in CustomItem.vb


Public Class CustomItem


Private _index As Integer


Public ReadOnly Property Index() As Integer


Get


Return _index


End Get


End Property


Public Sub New(ByVal Index As Integer)


_index = Index


End Sub


End Class



This implementation uses a simple array of the CustomItem type and offers nothing


beyond the For Each support. It can be easily extended to support additional array or


ArrayList methods, such as Count/Length and Item.The base collection type can also


be changed to ArrayList or HashTable for further functionality.


This implementation of the CustomItem type is no more than a contrived example


with a single property of Index. It should be extended to include the various properties



and methods relevant to the actual custom item.The construction of the item, or set of




items, can also come from an external data store.

No comments:

Variety in the Web World