Cannot iterate Hashtable in VBA (Excel)

后端 未结 2 636
Happy的楠姐
Happy的楠姐 2021-01-13 16:01

I am using Hashtable (mscorlib.dll referenced). I fill it with data, I can get any item (as long as I convert the request type to exactly same type what\'s stored in hashtab

2条回答
  •  春和景丽
    2021-01-13 16:12

    Actually there is one way to browse a .NET hashtable from VBA :

    Dim hash As mscorlib.Hashtable
    Set hash = New mscorlib.Hashtable
    
    Call hash.Add("foo", "bar")
    Call hash.Add(4, 8)
    
    Dim keys As IEnumerable
    Set keys = hash.keys
    
    Dim key As Variant
    For Each key In keys
    
        Dim value As Variant
    
        value = hash.Item(key)
    Next
    

    The main issue being "casting" the return value of the property "Keys" into a IEnumerable before using it in a "for each" loop. VBA can not handle multiple interface inheritance from scratch: you have to cast to the interface the function/property belongs to before you can call it.

提交回复
热议问题