How to create Collection Classes uisng COM

I want to create my own Collection Objects using COM.

Example

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

Company
 Department(Collection Of Departments)
  Employee (Collection Of Employee)
   
ArvindtnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

schutnikCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ArvindtnAuthor Commented:
It would have been ever more better if it was explained with a sample code.
0
schutnikCommented:
Sorry, don't have any handy now.

If you want, I can send you some tomorrow, just email me at kleahy@rgare.com


Kelly
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.