RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Feb 27, 2001

A template for building collection class modules

The following is a template for building collection class modules in a very quick and effective way. Copy-and-paste this code into Notepad, that save it to a file named "COLLECTION CLASS.CLS" in the \TEMPLATE\CLASSES subdirectory under the main VB6 directory:

  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
Attribute VB_Name = "CollectionClassName"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False

' A template for Collection Classes
' What to do after you add this template to the
' current program:
' 1) change the name of the class as needed
' 2) use the search and replace command to change all
'    instances of "BaseClassName" (without quotes), into
'    the actual name of the base class
' 3) optionally modify the ADD method to initialize
'    properties of the NEWITEM object before adding it
'    to the private collection
' 4) delete these remarks

Option Explicit

' The private collection used to hold the real data
Private m_PrivateCollection As Collection

Private Sub Class_Initialize()
    ' explicit assignment is slightly faster than auto-instancing
    Set m_PrivateCollection = New Collection
End Sub

' Add a new BaseClassName item to the collection

Public Sub Add(newItem As BaseClassName, Optional Key As Variant)
Attribute Add.VB_Description = "Adds a member to a Collection object"
    ' TO DO: initialize new item's properties here
    ' ...
    ' add to the private collection
    m_PrivateCollection.Add newItem, Key
End Sub

' Remove an item from the collection

Public Sub Remove(index As Variant)
Attribute Remove.VB_Description = "Removes a member from a Collection object"
    m_PrivateCollection.Remove index
End Sub

' Return a BaseClassName item from the collection

Function Item(index As Variant) As BaseClassName
Attribute Item.VB_Description = "Returns a specific member of a Collection " _
    & "object either by position or key"
Attribute Item.VB_UserMemId = 0
    Set Item = m_PrivateCollection.Item(index)
End Function

' Return the number of items in the collection

Property Get Count() As Long
Attribute Count.VB_Description = "Returns the number of members in a collection"
    Count = m_PrivateCollection.Count
End Property

' Remove all items from the collection

Public Sub Clear()
Attribute Clear.VB_Description = "Removes all members from a Collection object"
    Set m_PrivateCollection = New Collection
End Sub

' Implement support for enumeration (For Each)

Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
    ' delegate to the private collection
    Set NewEnum = m_PrivateCollection.[_NewEnum]
End Function
You can now add a new collection class by selecting the "Collection Class" template when you add a class module to your current project. If you don't see the list of templates, enforce this capability in the Environment tab of the Tools | Options dialog box.

After you've added the template to the project, follow the easy 4-step procedure described at the top of the file to turn it into a real collection class. Note that the template above works only under VB6. To have it work under VB5, you must delete the following four lines, near the beginning of the file:

  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
Francesco Balena
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date