全球收藏品

huangapple go评论74阅读模式
英文:

Global Collections

问题

Public myCol as new Collection:公开的 myCol 变量在模块顶部声明为一个新的集合。

我正在使用一个 Sub 来填充集合,然而这不会写入全局集合,在退出 Sub 后,内容会消失。

我假设项目,由于在子例程中创建,只有局部范围。

如何使用例程或函数来全局填充集合?

如果可能的话,我希望不必传递对象,因为我正在单个子例程中填充多个全局集合。

英文:

I have a collection that I have declared at the top of a module as

Public myCol as new Collection

I am using a Sub to populate the collection, however this does not write to the global collection and upon exiting the Sub the contents are gone.

I'm assuming the items, since created in the sub, only have local scope.

How can I use a routine or function to populate the collection globally?

I would prefer to not have to pass objects if possible as I am populating multiple global collections in the single subroutine.

答案1

得分: 2

创建一个标准模块来保存您的公共对象并通过属性公开它们。然后,属性的获取器可以检查对象是否已初始化,然后返回它。

请参考下面的示例:

Private m_collection As VBA.Collection

Public Property Get GlobalCollection() As VBA.Collection
    If m_collection Is Nothing Then Set m_collection = New VBA.Collection
    Set GlobalCollection = m_collection
End Property

调用它的方式:

YourModuleName.GlobalCollection.Add "Something"

或者

GlobalCollection.Add "Something"
英文:

Create a Standard module to hold your public objects and expose them via properties. The property getter can then check if the object has been initialized before returning it.

See an example:

Private m_collection As VBA.Collection

Public Property Get GlobalCollection() As VBA.Collection
    If m_collection Is Nothing Then Set m_collection = New VBA.Collection
    Set GlobalCollection = m_collection
End Property

To call it:

YourModuleName.GlobalCollection.Add "Something"

or

GlobalCollection.Add "Something"

huangapple
  • 本文由 发表于 2023年3月31日 17:48:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定