1

I am trying to create an array of dictionaries, since I need to create block of data divided by two like this :

 <START Block_1>
  <Name_1> <Value>
  <Name_2> <Value>
  <Name 3> <Value>
 <END Block_1>
 <START Block 2>
  <Name 1> <Value>
  <Name 2> <Value>
  <Name 3> <Value>
 <END Block_2>

I've already done all of the code to do this, the code is already creating the Lists with the values I need, but now I need to put this in an array, and this is where the problem begins.

I can do it, by doing it like this :

Dim myList : Set myList = CreateObject("Scripting.Dictionary")
myList.Add "Name1", "Value1"
myList.Add "Name2", "Value2"

Dim myArray : myArray = Array(myList)
MsgBox myArray(0).Item("Name1")

But then if I try to do it by creating an empty array and trying to fill it with the list I get an error. I am doing it like this :

Dim myList : Set myList = CreateObject("Scripting.Dictionary")
myList.Add "Name1", "Value1"
myList.Add "Name2", "Value2"

Dim myArray : myArray = Array()
ReDim Preserve myArray(1)
myArray(0) = myList
MsgBox myArray(0).Item("Name1")

And I get this error :

Wrong number of arguments or invalid property assignment

If I try to send a string in place of the list then I get the good result.

How can I solve this ?

Thank you in advance.

aliasbody
  • 816
  • 3
  • 11
  • 25

2 Answers2

2

You must use Set when assigning an object:

Option Explicit

ReDim a(-1) ' that's all what you need to create an empty dynamic/growable array
Dim d : Set d = CreateObject("Scripting.Dictionary")
d("a") = 1
d("b") = 2
ReDim Preserve a(UBound(a) + 1)
Set a(UBound(a)) = d
WScript.Echo "With Set => a(UBound(a))(""b""):", a(UBound(a))("b")

On Error Resume Next
a(UBound(a)) = d
WScript.Echo "No Set =>", Err.Number, Err.Description

output:

cscript aod.vbs
With Set => a(UBound(a))("b"): 2
No Set => 450 Wrong number of arguments or invalid property assignment

Added to explain the somewhat surprising error message:

Wrong use of Set will throw "Object required" errors most often (see here or here). I think that in this case the error message refers to "invalid property", because the interpreter looks for a non-object default property of the object right of the = when seeing statements like

variable = object

evidence:

Dim f : Set f = CreateObject("Scripting.FileSystemObject").GetFile("aod.vbs")
a(UBound(a)) = f ' f.Path is the default property!
WScript.Echo "No Set, but default property =>", a(UBound(a))

output:

No Set, but default property => E:\trials\SoTrials\answers\23847932\vbs\aod.vbs
Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
0

Creating arrays from a dictionary is the purpose of the dictionary object's Keys and Items methods.

Dim namesArray  : namesArray  = myList.Keys
Dim valuesArray : valuesArray = myList.Items

Now you have two physical matching arrays (one of "names" and one of "values") that would need to be combined into a single array in a loop using ReDim when declaring the array name to combine them.

Joe Weinpert
  • 56
  • 1
  • 1
  • 4