How to create Collection Classes uisng COM

I want to create my own Collection Objects using COM.


  A Company COM Object containing a collection of Department COM Objects, Each Department Containing a collection of Employee COM objects

 Department(Collection Of Departments)
  Employee (Collection Of Employee)
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

schutnikConnect With a Mentor Commented:
Very simple to implement...

You have to define a method called _NewEnum (exactly like that) with the dispid DISPID_NEWENUM (equals -4).  It should have the hidden attribute.

It should take one parameter (an out,retval param) as IUnknown **.

The parameter returned should be an object that exposes the IEnumVARIANT interface.

Implement this interface as follows...

method 'Next':
should take params celt, rgvar, pceltFetched as LONG, VARIANT *, LONG * and should return the next celt elements from the collection as VARIANT types in the memory pointed to by rgvar.  If celt is greater than 1, then the method should save the number of elements fetched in pceltFetched.  If celt is greater than 1 and pceltFetched is a null pointer, return E_INVALIDARG.  Obviously, it should increment an internal member variable that specifies the index of the next item to return.  Return S_OK if all requested elements are fetched, S_FALSE if not enough elements remain in array to fulfill request (partial or none at all)

method 'Reset':
should take no parameters and return S_OK.  should reset internal state to the same as when it was created (i.e. Index = 0)

method 'Clone':
should take 1 parameter, type IUnknown **.  should copy current state of object (that implements IEnumVARIANT) and return the new object pointer as IUnknown interface pointer in parameter.  (I implement this using a copy constructor and a QueryInterface call).  Return S_OK if successful.

method 'Skip':
should take 1 parameter, celt as LONG.  should perform same function as Next without fetching the values, and return S_OK if more elements remain in the collection or S_FALSE if collection is fully enumerated.

In my experience, I have never seen a VB client call anything other than Next (and only passing celt=1, pceltFetched=NULL for the params).  However, it would be safest to implement the object as described above.

This object should be a different object from your 'collection' itself.  I have actually written a few generic enumerators that I use in all of my projects (one that takes a static array of VARIANTS, one that is a Template and simply 'knows' how to get to the pieces of the collection)  I have also implemented these using one non-interface (non-COM) method in the collection object's underlying C++ class that returns the Count of elements in the collection, and one that returns an item from the collection (specified by index).  If you use this technique, be sure to check Count() each time you process a call to next because presumably, the collection's contents may have changed since the enumerator was created.

Good luck,
Kelly Leahy
ArvindtnAuthor Commented:
It would have been ever more better if it was explained with a sample code.
Sorry, don't have any handy now.

If you want, I can send you some tomorrow, just email me at

All Courses

From novice to tech pro — start learning today.