Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

vbscript - 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")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...