VBA子类参考 [英] VBA subclass reference
问题描述
在这篇文章中有对子类的描述在VBA中的用法。我正在寻找下一步:当我添加了第一个子项目时,我想使用它,但不知道如何使用。
In this post there is the description of "subclass" usage in VBA. I'm looking for the next step of it: when I have first subitem added I want to use it and don't know how.
当我写 baseItem(1).itemName
无效。
我认为这仅仅是因为baseItem不是集合或数组,但我不知道其他方法。
I assume it's just because that baseItem is not a collection or an array, but I don't know any other way.
推荐答案
我认为这仅仅是因为baseItem不是集合或
数组...
I assume it's just because that baseItem is not a collection or an array...
baseItem
本身不是集合,也不是数组。它只是 BaseClass
类型的对象。但是该对象 baseItem
包装了一个集合,因此我们也许可以说它几乎是一个集合。
The baseItem
itself is not a collection nor an array. It is just an object of type BaseClass
. But this object baseItem
wrapps a collection so we maybe could say it is almost a collection.
此对象的问题在于,它现在在您提到的答案,它无法提供客户端如何获取此集合的方法。需要修改类 BaseClass
,以便为该类的客户端提供对该 inner 集合的访问。通过 access 例如某些 public
函数的作用是从该集合中返回某些内容。
The problem with this object though is, that as it is defined now in the answer you mentioned, it provides no way how the clients can get to this collection. The class BaseClass
needs to be modified so it provides access to this inner collection for the client of this class. With access e.g. some public
function is meant which will return something from this collection.
向 BaseClass
添加类似内容:
Public Function getSubItem(index As Variant) As SubClass
Set getSubItem = subClassCollection.Item(index)
End Function
现在将基于该类定义在运行时创建的对象将通过此函数<$提供对 inner 集合的访问c $ c> getSubItem 。将使用此功能的代码如下所示。因此,您正在尝试实现的几乎是 。
Now objects which will be at runtime created based on this class definition will provide access to the inner collection via this function getSubItem
. The code which will use this function will look like this. So it is now almost that what you are trying to achieve.
Dim name As String
name = baseItem.getSubItem(1).itemName
Debug.Print name ' Prints "Something" in output window
中打印东西,但它甚至可以完全按照您想要的方式显示实现。导出 BaseClass.cls
文件并将 Attribute Value.VB_UserMemId = 0
添加到函数<$的开头时c $ c> getSubItem 并将其再次导入到项目中。
But it could be made even exactly to what you are trying to achieve. When exporting the file of BaseClass.cls
and adding Attribute Value.VB_UserMemId = 0
to the very beginning of function getSubItem
and importing it again to project.
Public Function getSubItem(index As Variant) As SubClass
Attribute Value.VB_UserMemId = 0
Set getSubItem = subClassCollection.Item(index)
End Function
现在您可以准确地编写代码了你想要的那种方式。 HTH
Now you can really write your code exactly that way you wanted. HTH
Dim name As String
name = baseItem(1).itemName
Debug.Print name ' Prints "Something" in output window
有关创建默认成员的更多信息在VBA
中,例如此处。
这篇关于VBA子类参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!