Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

A template for building collection class modules

Total Hit ( 3640)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
VERSION 1.0 CLASS
BEGIN
 MultiUse = -1 'True
 Persistable = 0 'NotPersistable
 DataBindingBehavior = 0 'vbNone
 DataSourceBehavior = 0 'vbNone
 MTSTransactionMode = 0 'NotAnMTSObject
END
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:

Click here to copy the following block
Persistable = 0 'NotPersistable
 DataBindingBehavior = 0 'vbNone
 DataSourceBehavior = 0 'vbNone
 MTSTransactionMode = 0 'NotAnMTSObject


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.