<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load()
Dim _customCollection = New CustomCollection(10)
DemoOutput.Text = ""
Dim _customItem As CustomItem
For Each _customItem In _customCollection
DemoOutput.Text += _customItem.Index & "<br />"
Next
End Sub
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
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
</script>
<html>
<head>
<title>Creating a Custom Collection</title>
</head>
<body>
<form id="MainForm" runat="server">
Output of Looping through a Custom Collection
<br />
<asp:literal id="DemoOutput" runat="server" />
</form>
</body>
</html>
|