Microsoft KB Archive/306961

= How to implement custom collections in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 306961

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q306961



For a Microsoft Visual C# .NET version of this article, see 307484.

For a Microsoft Visual C++ .NET version of this article, see 312514.

IN THIS TASK
SUMMARY
 * Implement the ICollection Interface
 * Implement an Enumerator Object for the GetEnumerator Method
 * Use For Each to Iterate Through the Custom Collection



SUMMARY
This step-by-step article shows you how to use the System.Collections.ICollection interface to implement and iterate through a custom collection in Visual Basic .NET or in Visual Basic 2005. The .NET Framework provides this interface to formally define size, enumerators, and synchronization methods for all collections. This article provides step-by-step instructions on how to implement the ICollection interface, how to implement an Enumerator object that can iterate through the entire collection, and how to use For Each to iterate through the collection.

back to the top

Implement the ICollection Interface
The ICollection interface inherits from the System.Collections.IEnumerable interface. ICollection defines a CopyTo method and three read-only properties: IsSynchronized, SyncRoot, and Count. ICollection inherits the GetEnumerator method from the IEnumerable interface. A class that intends to be a collection should implement the ICollection interface.

To implement the ICollection interface, follow these steps:  In Visual Basic .NET or in Visual Basic 2005, create a Windows application. In the Solution Explorer, right-click the project name, point to Add, and then click Add Class to add a class module named CustomCollection.  Add the following code to the beginning of the class module to import the System.Collection namespace: Imports System.Collections   Replace any other code in the module with the following code: Public Class CustomCollection Implements ICollection

Private intArr As Integer = {1, 3, 9} Private Ct As Integer Sub New Ct = 3 End Sub

End Class For simplicity, the CustomCollection class holds an array with three integer items and a count variable.   Implement the CopyTo method, which takes an Integer array and an index as parameters. This method copies the items in the collection to the array that is passed in. Paste the following code after the Sub New constructor: Public Sub CopyTo(ByVal myArr As Array, ByVal index As Integer) Implements ICollection.CopyTo Dim i As Integer For Each i In intArr myArr(index) = i       index = index + 1 Next End Sub   Implement GetEnumerator, which is inherited by the ICollection interface from IEnumerable. GetEnumerator returns an Enumerator object that can iterate through a collection. Paste the following code after the CopyTo method: Public Function GetEnumerator As IEnumerator Implements ICollection.GetEnumerator Return New Enumerator(intArr) End Function   Paste the following code after the GetEnumerator method to implement the three read-only properties: 'The IsSynchronized Boolean property returns True if the 'collection is designed to be thread safe; otherwise, it returns False. ReadOnly Property IsSynchronized As Boolean Implements ICollection.IsSynchronized Get Return False End Get End Property

'The SyncRoot property returns an object, which is used to synchronize 'the collection. This should return the instance of the object or return the 'SyncRoot of another collection if the collection contains other collections. ' ReadOnly Property SyncRoot As Object Implements ICollection.SyncRoot Get Return Me   End Get End Property

'The ReadOnly property Count returns the number 'of items in the custom collection. ReadOnly Property Count As Integer Implements ICollection.Count Get Return Ct   End Get End Property </li></ol>

back to the top

Implement an Enumerator Object for the GetEnumerator Method
This section demonstrates how to create an Enumerator class that can iterate through CustomCollection. <ol>  Paste the following code after the End Class statement in your class module: Class Enumerator Implements IEnumerator

Private intArr As Integer Private Cursor As Integer End Class The Enumerator class contains a private integer array intArr to hold the elements of the CustomCollection when the GetEnumerator method is called. The Cursor field member holds the current position while enumerating. </li>  Paste the following code after the members' declaration to add the following constructor to the class: Sub New(ByVal intArr As Integer) Me.intArr = intArr Cursor = -1 End Sub </li>  Paste the following code after the constructor to implement the Reset and MoveNext methods: Public Sub Reset Implements IEnumerator.Reset Cursor = -1 End Sub

Public Function MoveNext As Boolean Implements IEnumerator.MoveNext If Cursor < intArr.Length Then Cursor = Cursor + 1 End If

If (Cursor = intArr.Length) Then Return False Else Return True End If End Function Reset sets the cursor to -1. MoveNext moves the cursor to the next element and returns True if successful and False otherwise. </li>  Implement the read-only Current property, which returns the item that is pointed by the cursor. If the cursor is -1, it generates an InvalidOperationException class. Paste the following code after the MoveNext method: Public ReadOnly Property Current As Object Implements IEnumerator.Current Get If ((Cursor < 0) Or (Cursor = intArr.Length)) Then Throw New InvalidOperationException Else Return intArr(Cursor) End If    End Get End Property </li></ol>

back to the top

Use For Each to Iterate Through the Custom Collection
<ol> In Form1.vb, on the Design tab, drag a button onto the form.</li>  Double-click the button, and add the following code in the Click event of the button: Dim MyCol As CustomCollection Dim Myobj As Object MyCol = New CustomCollection

For Each Myobj In MyCol MessageBox.Show(Myobj.ToString) Next </li> Press the F5 key and click the button to run the application. Note that a message box displays the items in the custom collection.</li></ol>

How does this work? For Each calls the GetEnumerator method to create the Enumerator object and calls the MoveNext method to set the cursor to the first item. Then the Current property is accessed to get the item in MyObj. This is repeated until MoveNext returns False.

back to the top

Keywords: kbvs2005applies kbvs2005swept kbhowtomaster KB306961

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.