Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
MoveNext () gia tăng chỉ số và kiểm tra để xem liệu kết thúc của mảng đã được đạt tới: Công Chức năng MoveNext () Như Boolean _ Thực hiện System.Collections.IEnumerator.MoveNext chỉ số + = 1 Nếu chỉ số = ccol.Length Sau đó, Quay trở lại False Else Return True End Nếu Cuối Chức năng | 32 COLLECTIONS MoveNext increments the index and checks to see whether the end of the array has been reached Public Function MoveNext As Boolean _ Implements System.Collections.IEnumerator.MoveNext index 1 If index ccol.Length Then Return False Else Return True End If End Function The method returns False if the array is out of elements and True otherwise. The Current property is implemented as a read-only property since its only purpose is to return the current element of the array Public ReadOnly Property Current As Object _ Implements System.Collections.IEnumerator.Current Get Return ccol index End Get End Property The Reset method is implemented because the Enumerator class requires it. All it does is set the index back to -1 when appropriate. Now that we have all the code in place we can use the CEnumerator class to enumerate our custom collection of elements. We went to all the trouble to develop the class so that we can use a For Each statement to loop through the elements in the collection. When we use the For Each the enumerator for the collection is called and is used to enumerate the underlying array. One problem we ll see with this implementation is that every element of the array gets displayed even those elements that don t have a value. This will make your collection list look somewhat messy consequently you might want to decrease the initial capacity of the list. Let s put all this together and look at an example that demonstrates how to use the CEnumerator class by creating a list of names and displaying them A Collection Class Implementation Using Arrays 33 using a For Each statement Module Modulel Public Class CCollection Implements IEnumerable Protected pCapacity As Integer 8 Protected pArr 8 As Object Protected pIndex As Integer Protected pCount As Integer Public Sub New pIndex 0 pCount 0 End Sub ReadOnly Property Count Get Return pCount End Get End Property Public Sub Add ByVal item As Object If Me.IsFull Then pCapacity 8 ReDim Preserve pArr .