Click here to Skip to main content
12,826,806 members (33,374 online)
Click here to Skip to main content
Add your own
alternative version


47 bookmarked
Posted 20 Feb 2002

COM Collection

, 21 Feb 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
COM collection similar to the Visual Basic's Collection object
<!-- Add the rest of your HTML here -->


Visual Basic has a very nice object called Collection, which is part of VBA type library. In VC++ we do not have a similar object, which is a shame (in my opinion).

I searched MSDN and it turned out there is a sample called ATLCollections. This project has 2 collections. First is a collection of BSTRs and second one is a VARIANT collection which is using CComBSTRa as keys. I wanted to use only VARIANTs to be comparable with scripting languages so neither one was an option.

Next step was to search the Web. I found one on written by Chris Brooks ( and another one by Chris Sells ( Both were template-based and I did not want to use the templates. So I decided to write a collection object.

What is a collection?

According to MSDN a Collection object is an ordered set of items that can be referred to as a unit. The Collection object provides a convenient way to refer to a related group of items as a single object. The items, or members, in a collection need only be related by the fact that they exist in the collection. Members of a collection don't have to share the same data type.

A collection can be created the same way other objects are created. For example:

Dim X As New Collection

Once a collection is created, members can be added using the Add method and removed using the Remove method. Specific members can be returned from the collection using the Item method, while the entire collection can be iterated using the For Each...Next statement.

It means that a collection is a COM object that has 3 properties and 2 methods: Add, Remove, Count, Item and _NewEnum, though in technical literature you may find that only Count and _NewEnum are necessary for the object to be called the collection.

My collection defines the interface IXCollection implemented by the class CXCollection.

The IDL looks like this:

interface IXCollection : IDispatch
[id(1), helpstring("Adds a member to a collection")] 
HRESULT Add([in] VARIANT vItem, [in, optional] VARIANT vKey);

[id(2), helpstring("Removes a member from a collection")]
HRESULT Remove([in] VARIANT vIndex);

[propget, id(3), 
    helpstring("Returns the number of members in a collection")] 
HRESULT Count([out, retval] long * plNumber);

[propget, id(DISPID_VALUE), 
    helpstring("Returns a specific member of a collection "
    "either by position or key")] 
HRESULT Item([in] VARIANT vIndex, [out, retval] VARIANT * pvItem);

[propget, restricted, id(DISPID_NEWENUM), helpstring("Method _NewEnum")] 
HRESULT _NewEnum([out, retval] IUnknown ** ppUnk);

Now lets talk a little about each method and property.


Adds items to the collection. You can add items to the collection using the optional vKey parameter. If key is used the individual item can be accessed using this key.


Removes items from the collection either by the index or by the key.


Returns the number of items in the collection.


Returns an individual item either by the index or by the key. If the index is a number it’s 1-based. This property is given the standard DISPID DISPID_VALUE to make it as the "default" property to simplify its use in Visual Basic and scripting languages.


Returns an enumerator object that contains collection items allowing the client sequential access. This property is assigned the standard DISPID DISPID_NEWENUM, this DISPID is used by Visual Basic to implement its For-Each syntax.

The _NewEnum property returns an enumerator object that supports standard IEnumVARIANT interface. The IEnumVARIANT interface is a collection of Variants. It allows clients to enumerate heterogeneous collections of objects and intrinsic types when the clients cannot or do not know the specific type(s) of elements in the collection.

Source Code

The source code contains ShoppingCart project that implements the collection as well as C++ and VBScript test applications.


  • VC++ 6.0 SP4, Win2000 SP2.

For detailed coverage of the collections and enumerators I refer you to “ATL Internals” by B. Rector, C. Sells and “Professional ATL COM Programming” by R. Grimes.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Igor Vigdorchik
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

SuggestionCComEnumOnSTL for your convenience,... Pin
Vozzie224-May-13 16:44
memberVozzie224-May-13 16:44 
QuestionMore COM Collection in one interface Pin
Member 860584929-Jan-12 20:53
memberMember 860584929-Jan-12 20:53 
AnswerRe: More COM Collection in one interface Pin
swedish bricks19-Apr-12 11:03
memberswedish bricks19-Apr-12 11:03 
GeneralCOM Collection Protocol Pin
Vitaly Tomilov3-May-10 8:28
memberVitaly Tomilov3-May-10 8:28 
Questiona little doubt about IXCollection Pin
jeffrey.chou23-Aug-06 1:23
memberjeffrey.chou23-Aug-06 1:23 
AnswerRe: a little doubt about IXCollection Pin
jeffrey.chou28-Aug-06 1:12
memberjeffrey.chou28-Aug-06 1:12 
AnswerRe: a little doubt about IXCollection Pin
Igor Vigdorchik2-Sep-06 15:07
memberIgor Vigdorchik2-Sep-06 15:07 
GeneralRe: a little doubt about IXCollection Pin
XPointer2-Sep-06 17:46
memberXPointer2-Sep-06 17:46 
GeneralRe: a little doubt about IXCollection Pin
Igor Vigdorchik3-Sep-06 3:08
memberIgor Vigdorchik3-Sep-06 3:08 
QuestionCoding Error ? Pin
DarkByte2-Jul-04 18:56
memberDarkByte2-Jul-04 18:56 
AnswerRe: Coding Error ? Pin
Igor Vigdorchik6-Jul-04 15:45
memberIgor Vigdorchik6-Jul-04 15:45 
GeneralCXEnumVARIANT as coclass Pin
darkbyte30-Jun-04 14:33
memberdarkbyte30-Jun-04 14:33 
GeneralRe: CXEnumVARIANT as coclass Pin
Igor Vigdorchik30-Jun-04 18:16
memberIgor Vigdorchik30-Jun-04 18:16 
GeneralRe: CXEnumVARIANT as coclass Pin
BarkByte1-Jul-04 2:50
sussBarkByte1-Jul-04 2:50 
GeneralRe: CXEnumVARIANT as coclass Pin
Igor Vigdorchik1-Jul-04 6:42
memberIgor Vigdorchik1-Jul-04 6:42 
GeneralRe: CXEnumVARIANT as coclass Pin
DarkByte1-Jul-04 8:01
memberDarkByte1-Jul-04 8:01 
GeneralRe: CXEnumVARIANT as coclass Pin
Igor Vigdorchik1-Jul-04 8:19
memberIgor Vigdorchik1-Jul-04 8:19 
GeneralRational purify memory leak testing result Pin
fenil jacob27-Sep-03 3:55
memberfenil jacob27-Sep-03 3:55 
GeneralRe: Rational purify memory leak testing result Pin
Igor Vigdorchik27-Sep-03 5:31
memberIgor Vigdorchik27-Sep-03 5:31 
GeneralCode doesn't release com objects Pin
Roger Plant6-Jan-03 19:43
memberRoger Plant6-Jan-03 19:43 
GeneralRe: Code doesn't release com objects Pin
Igor Vigdorchik7-Jan-03 12:25
memberIgor Vigdorchik7-Jan-03 12:25 
GeneralMore Build Problems Pin
Eric Nichols7-May-02 5:36
memberEric Nichols7-May-02 5:36 
GeneralRe: More Build Problems Pin
Eric Nichols7-May-02 5:52
memberEric Nichols7-May-02 5:52 
GeneralBuild problems Pin
Eric Nichols6-May-02 10:56
memberEric Nichols6-May-02 10:56 
GeneralRe: Build problems Pin
Igor Vigdorchik6-May-02 13:11
memberIgor Vigdorchik6-May-02 13:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 22 Feb 2002
Article Copyright 2002 by Igor Vigdorchik
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid