1

I am trying to create a dictionary, with each key being a name I created, and each item an array of three objects: a string, an integer, and an array of what will be the results from a SOAP request.

I initialize the array like this:

Dim riskDict As New Scripting.Dictionary
riskDict.Add "Weight", Array("WP", 0, Array())

And I have a function, 'getTheRisk', which returns an X by 4 array (Its a SOAP request. I do not know how many results will come back, but it will be four values returned for each result.)

I would like to then store that result into the third element of the item in my dictionary, attempted like this:

For i = 0 To riskDict.count - 1
  riskDict.Items(i)(2) = getTheRisk(myDate.Value, myPort.Value, riskDict.Items(i)(0))
Next i

This compiles and runs just fine, but after assigning the array, it shows up as empty in the dictionary:

enter image description here

The getTheRisk function returns something like the following:

enter image description here

How do I set the array in the dict to be the results of the SOAP request?

lukehawk
  • 1,423
  • 3
  • 22
  • 48
  • You cannot access the elements of an array while it's stored in a dictionary. You need to pull it out into a temporary variable to modify it, then put it back – Tim Williams Dec 29 '15 at 17:02

1 Answers1

0

I found a workaround. I initialize the items with only two elements, then redim and add the third as I send the SOAP requests and get the results. I initialize as follows:

Dim riskDict As New Scripting.Dictionary
riskDict.Add "Weight", Array("WP", 0)

And for my loop:

For Each key In riskDict
  temp = riskDict(key)
  ReDim Preserve temp(UBound(temp) + 1)
  temp(UBound(temp)) = getTheRisk(myDate.Value, myPort.Value, riskDict(key)(0))
  riskDict(key) = temp
Next key

This is less than ideal, since the redim preserve. (And it just doesn't seem very elegant.) However, this dict is only about 8-10 items long, so it should not cause too much of a performance hit.

If you have any better solutions, please feel free to comment or post them.

Thanks all!

lukehawk
  • 1,423
  • 3
  • 22
  • 48
  • Looks fine, but you could also think of using `Collection` objects, which are polymorphic by nature and more easily expandable than arrays. – A.S.H Dec 29 '15 at 16:52
  • Store sub dictionaries in your dictionary. So you would have got a dictionary of dictionaries. – omegastripes Dec 29 '15 at 21:29