Is there a dictionary of dictionaries in QTP version of VBS?
Asked Answered
B

1

5

Something similar to Set<String, Set<String>> in Java?

Beckett answered 26/1, 2012 at 10:33 Comment(0)
G
12

A Set is an unordered collection of unique elements. Many Set implementations are based on hash tables (possibly of key-value pairs). VBScript has a Dictionary class -

Dim dicParent : Set dicParent = CreateObject("Scripting.Dictionary")

You can't add the same key twice, so the keys of a VBScript Dictionary represent/model a Set (the Set is ordered (by insertion), however). Nothing keeps you from putting (other) Dictionaries into the values:

>> Dim dicParent : Set dicParent = CreateObject("Scripting.Dictionary")
>> dicParent.Add "Fst", CreateObject("Scripting.Dictionary")
>> dicParent("Fst").Add "Snd", "child of parent"
>> WScript.Echo dicParent("Fst")("Snd")
>>
child of parent

In VBScript (and theory), you can even use objects as keys (not only strings as in other languages):

>> Dim dicParent : Set dicParent = CreateObject("Scripting.Dictionary")
>> Dim dicChild  : Set dicChild  = CreateObject("Scripting.Dictionary")
>> dicParent(dicChild) = "child of parent"
>> WScript.Echo dicParent(dicChild)
>>
child of parent

Your practical mileage may vary.

Gleet answered 26/1, 2012 at 11:29 Comment(2)
Nice remark about using objects as keys. Allthough, I'll have to think about a practical use for that. Maybe a poor man's linked list, stack or queue.Segmentation
Oh, I've used it to implement something like API caching -- to minimize references to the DataTable object, storing object properties (or references) in a dictionary. Works fine, can be useful indeed.Klaipeda

© 2022 - 2024 — McMap. All rights reserved.