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 TEMPLATECLASSES subdirectory under the main VB6 directory:

VERSION 1.0 CLASSBEGIN  MultiUse = -1  'True  Persistable = 0  'NotPersistable  DataBindingBehavior = 0  'vbNone  DataSourceBehavior  = 0  'vbNone  MTSTransactionMode  = 0  'NotAnMTSObjectENDAttribute VB_Name = "CollectionClassName"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = TrueAttribute VB_PredeclaredId = FalseAttribute 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 dataPrivate m_PrivateCollection As CollectionPrivate Sub Class_Initialize()    ' explicit assignment is slightly faster than auto-instancing    Set m_PrivateCollection = New CollectionEnd Sub' Add a new BaseClassName item to the collectionPublic 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, KeyEnd Sub' Remove an item from the collectionPublic Sub Remove(index As Variant)Attribute Remove.VB_Description = "Removes a member from a Collection object"    m_PrivateCollection.Remove indexEnd Sub' Return a BaseClassName item from the collectionFunction Item(index As Variant) As BaseClassNameAttribute 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 collectionProperty Get Count() As LongAttribute Count.VB_Description = "Returns the number of members in a collection"    Count = m_PrivateCollection.CountEnd Property' Remove all items from the collectionPublic Sub Clear()Attribute Clear.VB_Description = "Removes all members from a Collection object"    Set m_PrivateCollection = New CollectionEnd Sub' Implement support for enumeration (For Each)Function NewEnum() As IUnknownAttribute NewEnum.VB_UserMemId = -4Attribute 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

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: