p;IsDone() As Boolean
Return current >= aggregate.Count
End Function
End Class
一个实例
下面的迭代器代码用来演示跳过某些项目遍历一个集合。
实例代码
'' MainApp test application
Module MainApp
Sub Main()
'' Build a collection
Dim collection As New Collection()
collection(0) = New Item("Item 0")
collection(1) = New Item("Item 1")
collection(2) = New Item("Item 2")
collection(3) = New Item("Item 3")
collection(4) = New Item("Item 4")
collection(5) = New Item("Item 5")
collection(6) = New Item("Item 6")
collection(7) = New Item("Item 7")
collection(8) = New Item("Item 8")
'' Create iterator
Dim iterator As New Iterator(collection)
'' Skip every other item
iterator.[Step] = 2
Console.WriteLine("Iterating over collection:")
Dim item As Item = iterator.First()
While Not iterator.IsDone
Console.WriteLine(item.Name)
item = iterator.[Next]()
End While
'' Wait for user
Console.Read()
End Sub
End Module
Public Class Item
Private m_name As String
'' Constructor
Public Sub New(ByVal name As String)
Me.m_name = name
End Sub
'' Property
Public ReadOnly