Can a class extend the Collection object? Can a class extend the Collection object? vba vba

Can a class extend the Collection object?


You are running into one of the limitations of Implements in VBA. You can't implement another class if the other class has any public methods or properties with an underscore in the name. Collection class of course has _NewEnum but any underscore will cause a problem.

For example, if you created a class AddressClass that had the following:

Public Address_City As String

Then created another class CustomerAddress:

Implements AddressClassPrivate Property Get ClassInterface_Address_City() As StringEnd PropertyPrivate Property Let ClassInterface_Address_City(ByVal RHS As String)End Property

When you compile, you will get an error "Object module needs to implement 'Address_City' for interface 'AddressClass'." Changing the property to AddressCity makes the error go away.

Possible solution: If I understand correctly, you want to implement the collection class so you can pass your new class to methods that take in collections as parameters. Is it possible to alter those methods? My suggestion would be to create your own collection class MyCollection and then implement it instead. i.e. UniformMyCollection That way you can completely avoid problems with underscores.

As for Count, I would trust the Object Browser over the help text anytime. On the other hand, if you are creating your own collection class, it doesn't matter which one you choose.


VBA has a lot of limitations on what classes you can implement. The NewEnum is tripping up Collection, but even if it wasn't, there could very well be something else in that class to trip it up. I think it reports the first problem it finds.

Because Collection has so few properties and methods, I just rewrite them.

Private mcolParts As CollectionPublic Sub Add(clsPart As CPart)    mcolParts.Add clsPart, CStr(clsPart.PartID)End SubPublic Property Get Count() As Long    Count = mcolParts.CountEnd PropertyPublic Property Get Item(vItm As Variant) As CPart    Set Item = mcolParts.Item(vItm)End PropertyPublic Sub Remove(vIndex As Variant)    mcolParts.Remove vIndexEnd Sub

In don't know why the OB shows methods (they look like green boxes to me). For my money, methods either change multiple properties or interact with something outside of the class. Everything else is a property. I'd call both Count and Index properties.


Dick Kusleika has most of it, but if you want to use For Each on your custom class, you'll also need:

'--- required additional property that allow to enumerate the collection with For EachPublic Property Get NewEnum() As IUnknown    Set NewEnum = m_ColParts.[_NewEnum]End Property

This isn't discussed in either of the links I found in my Favorites (this one or this one), but they're both worth reading. If I find the site that talks about NewEnum I'll do an Edit to add it.

EDIT

Neither of these links are the one I was looking for, either, but both discuss the NewEnum property (including a little extra voodoo that neeeds to be added):

Here and here.

Both of these talk about Excel, but the VBA is the same in other Office applications (including the need for the export->text edit->import process to get "Attributes").