Can a class extend the Collection object?

collections, interface-implementation, vba

Solution

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 AddressClass

Private Property Get ClassInterface_Address_City() As String
End Property

Private 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.

Problem

I'm trying to extend functionality of the VBA `Collection` object in a new class and make this class an inheritant of `Collection`, but the `Implements Collection` statement gives me the following error: Bad interface for Implements: method has underscore in its name. What underscore?! `Add`, `Item`, `Remove`, and `Count` are the only methods listed in the documentation for `Collection`. All four are underscore-free. EDIT: To clarify, I'm making a class called `UniformCollection` (that only accepts members that are all of the same type, inspired by this approach). I'd like it to implement `Collection`, so that a `UniformCollection` is a `Collection` and can be used in place of a `Collection` when calling other objects' methods, etc. I know I have to write delegating methods/properties for Add, Item, etc., and a NewEnum property for `For Each` to work, and I've done so already. My problem is that the `Implements Collection` statement gives me the error stated above. Bonus question: is `Count` a method or a property of `Collection`? Help calls it a property, but the Object Browser in the VBA editor calls it a function i.e. method (flying yellow box).

Original source