类可以扩展 Collection 对象吗?
我试图在新类中扩展 VBA Collection
对象的功能,并使该类成为 Collection
的继承者,但 Implements Collection
声明给了我以下错误:
Implements 的错误接口:方法 名称中有下划线。
什么下划线?! Add
、Item
、Remove
和 Count
是 Collection< 文档中列出的唯一方法/代码>。所有四个都没有下划线。
编辑:为了澄清,我正在创建一个名为 UniformCollection
的类(仅接受所有相同类型的成员,灵感来自 这种方法)。我希望它实现 Collection
,以便 UniformCollection
是一个 Collection
> 并且可以在调用其他对象的方法等时代替 Collection
使用。
我知道我必须为 Add、Item 等编写委托方法/属性,并为 < 编写 NewEnum 属性code>For Each 工作,我已经这样做了。
我的问题是 Implements Collection
语句给了我上述错误。
额外问题:Count
是 Collection
的方法还是属性?帮助将其称为属性,但 VBA 编辑器中的对象浏览器将其称为函数,即方法(飞行的黄色框)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您遇到了 VBA 中 Implements 的限制之一。如果另一个类具有名称中带有下划线的任何公共方法或属性,则您无法实现另一个类。
Collection
类当然有_NewEnum
但任何下划线都会导致问题。例如,如果您创建了一个具有以下内容的类
AddressClass
:然后创建了另一个类
CustomerAddress
:编译时,您将收到错误“对象模块需要实现 '接口“AddressClass”的“Address_City”。”将属性更改为
AddressCity
可使错误消失。可能的解决方案:如果我理解正确,您想要实现集合类,以便可以将新类传递给以集合作为参数的方法。是否可以改变这些方法?我的建议是创建您自己的集合类
MyCollection
然后实现它。即UniformMyCollection
这样就可以完全避免下划线的问题。至于
Count
,我随时都会相信对象浏览器而不是帮助文本。另一方面,如果您要创建自己的集合类,那么选择哪个并不重要。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:Then created another class
CustomerAddress
: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 对于可以实现的类有很多限制。 NewEnum 会导致 Collection 出错,但即使不是,该类中也很可能有其他东西会导致它出错。我认为它报告了它发现的第一个问题。
因为 Collection 的属性和方法太少,所以我只是重写它们。
不知道为什么 OB 显示方法(对我来说它们看起来像绿色框)。对于我来说,方法要么改变多个属性,要么与类之外的东西交互。其他一切都是财产。我将同时调用 Count 和 Index 属性。
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.
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 拥有大部分内容,但如果您想在自定义类上使用
For Each
,您还需要:我在收藏夹 (< a href="http://www.cpearson.com/excel/CollectionClass.aspx" rel="nofollow noreferrer">这个 或 这个),但他们两者都值得一读。如果我找到谈论 NewEnum 的网站,我将进行编辑以添加它。
编辑
这些链接都不是我要找的链接,但都讨论了 NewEnum 属性(包括需要添加的一点额外的巫术):
此处 和
在这里。
这两者都谈论Excel,但VBA在其他Office应用程序中是相同的(包括需要导出->文本编辑->导入过程来获取“属性”)。
Dick Kusleika has most of it, but if you want to use
For Each
on your custom class, you'll also need: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").
Re RolandTumble 关于“NewEnum”的注释:
我自己在 Access 2003 中的经验是,“For Each”通过导入包括该行的代码可以正常工作
...但是在我“/反编译”该文件(命令行开关)之后,该行已被已删除(导出时验证)并且“For Each”不起作用。
不幸的是,当“压缩和修复”无法为我解决问题时,我需要使用“/反编译”。
Re RolandTumble's note on "NewEnum" :
My own experience in Access 2003 is that "For Each" works fine by importing code including the line
... but after I "/decompile" the file (command line switch), the line has been removed (verified on export) and the "For Each" does not function.
Unfortunately I need to use "/Decompile" when "Compress and Repair" does not fix things for me.