I have a nested tree of nodes that are either Dictionaries
or Collections
(I don't have control over this structure - it is given to me). How can I seperate Dictionary
nodes from Collections
?
I observe there exists an IsArray()
function, but no IsCollection
or IsDict()
You can assign a Key to an Item when you add the Item to the Collection, but you cannot retrieve the Key associated with an Item nor can you determine (directly) whether a key exists in a Collection. Dictionaries are much friendly and open with their keys. Dictionaries are also considerably faster than Collections.
A dictionary is an object in VBA (similar to a collection) that stores data like an array. It can be used to store data of any type except an array. A VBA dictionary has two parts: Key. Value.
Sub TestingType()
Dim col As New Collection
Dim dic As New Scripting.Dictionary
Debug.Print TypeName(col) 'Collection
Debug.Print TypeName(dic) 'Dictionary
End Sub
Try something like this:
If TypeOf YourObjectVariable Is Dictionary Then
' ...
ElseIf TypeOf YourObjectVariable Is Collection Then
' ...
Else
' Handle empty/other types here.
End If
You could use something like this to control the flow of execution in your code, or make your own IsCollection() and IsDictionary() functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With