vba 获取 Collection 对象上项目的键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/738412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:43:08  来源:igfitidea点击:

Get the key of an item on a Collection object

vbacollectionsvb6excel-vbaexcel

提问by jpinto3912

The environment is that the members I'm pushing into the Collection are nameless, un-identifiable (to avoid bad abstractions, and please, don't freak out: members are actually other Collection instances). In order to be able to make fast searches, I'm creating a meaningfull hash name for each new member, and provide it as the Key string, on the Add method of the "topmost" Collection.

环境是我推入 Collection 的成员是无名的、无法识别的(为了避免错误的抽象,请不要惊慌:成员实际上是其他 Collection 实例)。为了能够进行快速搜索,我为每个新成员创建了一个有意义的哈希名称,并在“最顶层”集合的 Add 方法上将其作为 Key 字符串提供。

When I have a key to seach with, everything's dandy... Problem is I'd like to iterate the members of the collection and get the Key that was provided on Add (the generated Hash, that unfortunetely is not possible to reverse-Hash).

当我有一个要搜索的密钥时,一切都很花哨......问题是我想迭代集合的成员并获取在 Add 上提供的密钥(生成的哈希,不幸的是不可能反向哈希)。

I'm moving on by defining that the first member of the inserted sub-collection instance is a string, containing the mentioned hash, but if anyone cracks this, I'll be much obliged.

我继续定义插入的子集合实例的第一个成员是一个字符串,包含提到的散列,但如果有人破解这个,我将非常感激。

回答by cmsjr

The simple approach would be to use a Dictionaryinstead of a Collection. The Dictionary is essentially an associative array of key, item pairs and support retrieval of its keys as an array. To use the Dictionary you will need to add a reference to the Microsoft Scripting Runtime. The drawback to using the dictionary is that it is not enumerable in the same way as the collection. A more elaborate solution would be to wrap the collection and dictionary to create an enumerable dictionary as outlined below.

简单的方法是使用字典而不是集合。Dictionary 本质上是一个键、项目对的关联数组,并支持将其键作为数组检索。要使用词典,您需要添加对 Microsoft Scripting Runtime 的引用。使用字典的缺点是它不像集合那样可枚举。一个更复杂的解决方案是包装集合和字典以创建一个可枚举的字典,如下所述。

NBTo get NewEnum to work properly in VBA the class module has to be exported and manually edited as follows and then re-imported.

注意要让 NewEnum 在 VBA 中正常工作,必须按如下方式导出和手动编辑类模块,然后重新导入。

Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
   Set NewEnum = someKeys.[_NewEnum]
End Property

example

例子

Option Explicit
Private someKeys As Dictionary
Private someCols As Collection
Public Function Add(o As Object, Key As String) As Object
    someKeys.Add Key, o
    someCols.Add o, Key
End Function
Public Property Get Count() As Long
    Count = someCols.Count
End Property
Public Property Get Item(vKey As Variant) As Object
    Set Item = someCols.Item(vKey)
End Property
Public Sub Remove(vKey As Variant)
    someKeys.Remove vKey
    someCols.Remove vKey
End Sub
Public Property Get NewEnum() As IUnknown
   Set NewEnum = someCols.[_NewEnum]
End Property
Public Property Get Keys() As Variant
    Keys = someKeys.Keys
End Property
Private Sub Class_Initialize()
    Set someKeys = New Dictionary
    Set someCols = New Collection
End Sub

回答by jpinto3912

You could wrap your member collections in you own collection call that store the key and the collection. The is just a matter of iterating throw the collection and asking the elements for their key (i.e. you hash).

您可以将您的成员集合包装在您自己的集合调用中,用于存储密钥和集合。这只是迭代抛出集合并询问元素的键(即散列)的问题。

回答by angelo.mastro

try to substitute

尝试替代

Public Function NewEnum() As IUnknown
        Set NewEnum = m_ObjectCollection.[_NewEnum]
End Function

with

Public Function NewEnum() As Collection
        Set NewEnum = m_ObjectCollection
End Function

it worked for me.

它对我有用。