user1925406
user1925406

Reputation: 733

Is it possible to have a array inside a Dictionary object

I knew that combining to similar data structures wouldn't be logical but curious to know if we can have a array element inside Dictionary object. some thing like:

Set d = CreateObject("Scripting.Dictionary")

d.Add "Name", "John"
d.Add "Age", 31
d.Add "Company", Array("microsoft", "apple")

Upvotes: 4

Views: 10175

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

You are one WScript.Echo away from solving the question for yourself:

>> Set d = CreateObject("Scripting.Dictionary")
>> d.Add "Company", Array("microsoft", "apple")
>> WScript.Echo Join(d("Company"))
>>
microsoft apple

cf this question

Update (thanks to @Ansgar):

The elements delivered by .Item() (and For Each) are copies; and Array assignment copies too (does not take a reference as in other languages). So changing an array element stored in a dictionary means assigning a new array:

>> Set d = CreateObject("Scripting.Dictionary")
>> d.Add "Company", Array("microsoft", "apple")
>> WScript.Echo Join(d("Company"))
>> d("Company") = Array(d("Company")(1), "samsung")
>> WScript.Echo Join(d("Company"))
>>
microsoft apple
apple samsung

Sometimes it's more convenient to use a(nother) dictionary, a System.Collections.Arraylist, or a custom object (all objects are references, so an assignment gives access to the original element).

Upvotes: 8

Related Questions