xijhoy
xijhoy

Reputation: 177

Creating dictionary in VBScript

I created dictionary by this way:

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

This is ordinary dictionary. It is looks like

key1    value1
key2    value2

After that I added this code:

Dim var
var = "test"

Set d(var) = CreateObject("Scripting.Dictionary")
d(var).Add "x", "ValueX"
WScript.Echo d(var).Item("x")

And got "ValueX"

What is this d(var)? How it relates to the dictionary d?

Upvotes: 0

Views: 976

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200283

Your dictionary d contains a nested dictionary in the key test to which you add a key x with the value ValueX. It probably becomes clearer when you visualize the data structure (you can use my helper function DumpData() for that):

>>> import "C:\Temp\DataDumper.vbs"
>>> Set d = CreateObject("Scripting.Dictionary")
>>> WScript.Echo DumpData(d)
{}
>>> var = "test"
>>> Set d(var) = CreateObject("Scripting.Dictionary")
>>> WScript.Echo DumpData(d)
{
        "test" => {}
}
>>> d(var).Add "x", "ValueX"
>>> WScript.Echo DumpData(d)
{
        "test" => {
                "x" => "ValueX"
        }
}

Upvotes: 2

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

d is a dictionary. It contains a key ("test") - value (d("test")/d.Item("test")) pair. The value of this pair happens to be a dictionary too. That contains a pair "x" - "ValueX".

Upvotes: 1

Related Questions