Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleBuild a custom collection class that supports For Each
Keywordscustom collection, For Each, ForEach
CategoriesClasses
 
In the custom collection class, use a private (normal) Collection object to store the items in the collection. Write subroutines that implement the Add, Remove, Count, and Item methods.
 
Private m_Items As Collection

' Add an item to the collection.
Public Sub Add(ByVal Item As Variant, Optional ByVal key As _
    Variant, Optional ByVal before As Variant, Optional _
    ByVal after As Variant)
    m_Items.Add Item, key, before, after
End Sub

' Return the number of items.
Public Function Count() As Integer
    Count = m_Items.Count
End Function

' Remove an item.
Public Sub Remove(ByVal Index As Variant)
    m_Items.Remove Index
End Sub

' Return the indicated item.
Public Function Item(ByVal Index As Variant) As Variant
    Item = m_Items.Item(Index)
End Function
 
There are two features of a normal Collection that the custom collection still doesn't support: a default method and For Each.

Default Method

First, it doesn't have a default method. That means you can access an item as in:

    value = custom_collection.Item(1)

But you cannot get theitem like this:

    value = custom_collection(1)

Follow these steps to make the Item function the colection's default method.

  1. Open the code window for the custom collection class.
  2. Select the Tools menu's Procedure Attributes item.
  3. On the dialog, click the Advanced >> button.
  4. In the Name dropdown, select the Item function.
  5. In the ProcedureId box, select (Default).
  6. Click OK.

For Each

The custom collection also doesn't support For Each. To use a For Each loop, Visual Basic needs an enumerator function to help it loop through the items in the collection. If you try to execute a For Each loop on the custom collection at this point, you'll receive the error:

    Object doesn't support this property or method

The only way to provide this function in Visual Basic 6 is to use the enumerator of a normal Collection object. This is the reason the custom collection stores its values in a private Collection. If it were not for this issue, you could store the values in some other data structure such as a tree, linked list, hash table, or whatever.

To provide the enumerator function, add the following property procedure:

 
' Support For Each.
Public Property Get NewEnum() As IUnknown
    Set NewEnum = m_Items.[_NewEnum]
End Property
 
Now follow these steps to tell Visual Basic which routine provides the enumerator:

  1. Open the code window for the custom collection class.
  2. Select the Tools menu's Procedure Attributes item.
  3. On the dialog, click the Advanced >> button.
  4. In the Name dropdown, select the NewEnum function.
  5. In the ProcedureId box, enter -4.
  6. Click OK.

See Also

HowTo: Build a strongly typed collection class

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated