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).
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.
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 Collection
Public Sub Add(clsPart As CPart)
mcolParts.Add clsPart, CStr(clsPart.PartID)
End Sub
Public Property Get Count() As Long
Count = mcolParts.Count
End Property
Public Property Get Item(vItm As Variant) As CPart
Set Item = mcolParts.Item(vItm)
End Property
Public Sub Remove(vIndex As Variant)
mcolParts.Remove vIndex
End 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With