Get the item key in a Collection object

The environment is that the members that I insert into the collection are nameless, not identifiable (to avoid bad abstractions and please don't worry: members are actually other instances of the collection). To be able to perform a quick search, I create a multi-valued hash name for each new member and provide it as a key string in the Add method of the "top" collection.

When I have a key to talk, all dandy ... The problem is that I would like to repeat the elements of the collection and get the Key that was provided in Add (created by Hash, which, unfortunately, is impossible for reverse-Hash).

I continue by determining that the first member of the inserted instance of the subnets is a string containing the mentioned hash, but if someone breaks this, I will be very grateful.

+5
source share
3 answers

. , . , Microsoft Scripting Runtime. , , . , .

NB NewEnum VBA, , .

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

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
+6

, . , (.. ).

+2

try replacing

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

from

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

it worked for me.

0
source

All Articles