Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a array inside a Dictionary object

Tags:

vbscript

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")
like image 967
user1925406 Avatar asked Feb 18 '23 09:02

user1925406


1 Answers

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).

like image 57
Ekkehard.Horner Avatar answered Mar 07 '23 09:03

Ekkehard.Horner