Array within a dictionary

J

jrpfinch

Please could somebody help me why this code does not work as expected
in Excel 2003:

Sub Test()
Dim dic As Dictionary
Set dic = New Dictionary

dic("Hello") = Array("Zero", "Item1")
Debug.Print dic("Hello")(0)
Debug.Print dic("Hello")(1)

dic("Hello")(1) = dic("Hello")(1) & ",Item2"
Debug.Print dic("Hello")(1)

End Sub

Output:
Zero
Item1
Item1





Thanks

Jon
 
J

Jarek Kujawa

this code:

Sub Test()
Dim hello()


hello = Array("Zero", "Item1")
Debug.Print hello(0)
Debug.Print hello(1)


hello(1) = hello(1) & ",Item2"
Debug.Print hello(1)


End Sub

will bring:
Zero
Item1
Item1,Item2

as the results
 
D

Dana DeLouis

I believe that in order to change an item in the array, the entire array
needs to be changed at the same time.
If I understand the question correctly, here is one idea...

Sub Demo()
Dim Dic
Dim M 'Matrix Array

Set Dic = CreateObject("Scripting.Dictionary")

' Add Key, Item (Both Required)
Dic.Add "Hello", Array("Zero", "Item1")

Debug.Print Dic("Hello")(0)
Debug.Print Dic("Hello")(1)

'Get Data to change
M = Dic("Hello")
'New Data
M(1) = M(1) & ", Item2"
'Change it
Dic("Hello") = M

Debug.Print Dic("Hello")(0)
Debug.Print Dic("Hello")(1)
End Sub

= = = = =
HTH
Dana DeLouis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top