类可以扩展Collection对象吗? [英] Can a class extend the Collection object?

查看:77
本文介绍了类可以扩展Collection对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在新类中扩展VBA Collection 对象的功能,并使该类成为 Collection ,但是 Implements Collection 语句给我以下错误:

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.

什么是下划线?! 添加项目删除 Count Collection 文档中列出的唯一方法。所有这四个都不包含下划线。

What underscore?! Add, Item, Remove, and Count are the only methods listed in the documentation for Collection. All four are underscore-free.

编辑:为澄清起见,我正在制作一个名为 UniformCollection 的类(仅接受受此方法启发的相同类型的成员 )。我希望它实现 Collection ,这样一个 UniformCollection Collection ,并且可以在调用其他对象的方法等时代替 Collection 使用。

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.

我知道我必须为Add,Item等编写委托方法/属性,并为 For Each 上班,而我已经这样做了。

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.

奖金问题:是 Count 个<$ c $方法或属性c>收藏?帮助将其称为属性,但VBA编辑器中的对象浏览器将其称为函数(即方法)(显示黄色框)。

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

推荐答案

您正在遇到VBA中Implements的局限性之一。如果其他类具有名称中带有下划线的任何公共方法或属性,则您无法实现该其他类。 Collection 类当然具有 _NewEnum ,但是任何下划线都会引起问题。

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.

例如,如果您创建具有以下内容的类 AddressClass

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

Public Address_City As String

然后创建了另一个类 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

编译时,会出现错误对象模块需要为接口'AddressClass'实现'Address_City'。将属性更改为 AddressCity 可使错误消失。

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.

可能的解决方案:我理解正确,您想实现集合类,以便可以将新类传递给将集合作为参数的方法。有可能改变那些方法吗?我的建议是创建自己的集合类 MyCollection ,然后实施它。即 UniformMyCollection 这样,您就可以完全避免下划线出现问题。

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.

对于 Count ,我会随时通过帮助文本信任对象浏览器。另一方面,如果您要创建自己的集合类,那么选择哪个都没关系。

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.

这篇关于类可以扩展Collection对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆