类可以实现Collection对象吗? [英] Can a class implement the Collection object?

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

问题描述

我试图在新类中扩展VBA Collection 对象的功能,并使这个类成为 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.

EDIT :为了说明,我创建了一个名为 UniformCollection 只接受属于同一类型的成员,受到这个方法)。我想要实现 Collection ,以便 UniformCollection a 集合,并可在调用其他对象的方法等时用于替换集合

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等写入委托方法/属性,以及的NewEnum属性 $ c>工作,我已经这样做了。

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.

我的问题是 Implements Collection 语句给我上面的错误。

My problem is that the Implements Collection statement gives me the error stated above.

奖金问题计数一种方法或属性 Collection ?帮助将其称为属性,但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中实现的限制之一。如果其他类具有任何公共方法或名称中带有下划线的属性,则不能实现另一个类。 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.

对于 / code>,我会在任何时候相信帮助文本上的对象浏览器。另一方面,如果您正在创建自己的集合类,那么您选择哪一个无关紧要。

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天全站免登陆