2

Simple example:

Dim d, a(0)
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
Set a(0) = d

WScript.Echo(TypeName(a))

Output:

Variant()

I don't see any way how to access this object, it counts 3, but it seems like empty

I find it surprising that Google does not list any useful result for this question - how to assign dictionary in array?

Closest I found was this unanswered link

My problem is that I have a loop that creates temporary dictionary, which I planned to feed in simple one dimensional array with dictionary objects, but without success

I also found something like this link where two arrays are created for dictionary keys and for dictionary items (values) separately, which just complicated things too much for my case

So is this possible at all with VBScript?

2 Answers 2

3

I might have misunderstood your question, but you can access the Dictionary like this:

WScript.Echo(a(0).Item("b"))

Edit

This code:

Dim d, a(0)
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
Set a(0) = d
WScript.Echo(a(0).Item("b"))

dim x
x = d.Items

dim i
for i = lbound(x) to ubound(x)
    WScript.Echo(x(i))
next

Produces this output on my Windows 7 machine:

C:\>cscript test.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Belgrade
Athens
Belgrade
Cairo

C:\>
Sign up to request clarification or add additional context in comments.

4 Comments

Just like I wanted, but unfortunately it throws an error: Type mismatch
I've added a complete example that includes version numbers, what version of vbscript are you using?
Sorry, it was typo at my end (my dict was d and I just pasted your answer) It works perfectly, thanks
What helped me was the "set" command, i was doing a(0) = d but when I switched it to set a(0) = d, it worked.
0

An example :

Dim d
Set d = CreateObject("Scripting.Dictionary") 
d.Add "a", "Athens" 
d.Add "b", "Belgrade" 
d.Add "c", "Cairo" 

WScript.Echo(d("a")) '=> Athens

d("a") = "Brussels"

WScript.Echo(d("a")) '=> Brussels

a = d.Keys   ' Get the keys.
b = d.Items
For i = 0 To d.Count -1 ' Iterate the array.
  wscript.echo a(i) & " " & b(i)
Next


'=>a Brussels
'=>b Belgrade
'=>c Cairo

1 Comment

Yes, but dividing keys/items in separate arrays complicates a bit when using loop and dict has variable keys. In simple example it's clear of course. Thanks guys, I found the problem I had with your answers - if looping over temp dict and assigning it to array, dict has to be destroyed after adding it to array and Dictionary object recreated again. I was using dict.RemoveAll instead, and got into this trouble. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.