类可以扩展 Collection 对象吗?

发布于 2024-11-02 21:48:47 字数 978 浏览 8 评论 0原文

我试图在新类中扩展 VBA Collection 对象的功能,并使该类成为 Collection 的继承者,但 Implements Collection声明给了我以下错误:

Implements 的错误接口:方法 名称中有下划线。

什么下划线?! AddItemRemoveCountCollection< 文档中列出的唯一方法/代码>。所有四个都没有下划线。

编辑:为了澄清,我正在创建一个名为 UniformCollection 的类(仅接受所有相同类型的成员,灵感来自 这种方法)。我希望它实现 Collection,以便 UniformCollection 是一个 Collection > 并且可以在调用其他对象的方法等时代替 Collection 使用。

我知道我必须为 Add、Item 等编写委托方法/属性,并为 < 编写 NewEnum 属性code>For Each 工作,我已经这样做了。

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

额外问题CountCollection 的方法还是属性?帮助将其称为属性,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赴月观长安 2024-11-09 21:48:47

您遇到了 VBA 中 Implements 的限制之一。如果另一个类具有名称中带有下划线的任何公共方法或属性,则您无法实现另一个类。 Collection 类当然有 _NewEnum 但任何下划线都会导致问题。

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

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 可使错误消失。

可能的解决方案:如果我理解正确,您想要实现集合类,以便可以将新类传递给以集合作为参数的方法。是否可以改变这些方法?我的建议是创建您自己的集合类 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:

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.

挽袖吟 2024-11-09 21:48:47

VBA 对于可以实现的类有很多限制。 NewEnum 会导致 Collection 出错,但即使不是,该类中也很可能有其他东西会导致它出错。我认为它报告了它发现的第一个问题。

因为 Collection 的属性和方法太少,所以我只是重写它们。

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

不知道为什么 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.

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.

只是在用心讲痛 2024-11-09 21:48:47

Dick Kusleika 拥有大部分内容,但如果您想在自定义类上使用 For Each,您还需要:

'--- required additional property that allow to enumerate the collection with For Each
Public Property Get NewEnum() As IUnknown
    Set NewEnum = m_ColParts.[_NewEnum]
End Property

我在收藏夹 (< 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:

'--- required additional property that allow to enumerate the collection with For Each
Public Property Get NewEnum() As IUnknown
    Set NewEnum = m_ColParts.[_NewEnum]
End Property

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

山川志 2024-11-09 21:48:47

Re RolandTumble 关于“NewEnum”的注释:

我自己在 Access 2003 中的经验是,“For Each”通过导入包括该行的代码可以正常工作

Attribute NewEnum.VB_UserMemId = -4

...但是在我“/反编译”该文件(命令行开关)之后,该行已被已删除(导出时验证)并且“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

Attribute NewEnum.VB_UserMemId = -4

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文