ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Array within a dictionary (https://www.excelbanter.com/excel-programming/436428-array-within-dictionary.html)

jrpfinch

Array within a dictionary
 
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

Jarek Kujawa[_2_]

Array within a dictionary
 
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


On 20 Lis, 13:54, jrpfinch wrote:
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



Dana DeLouis

Array within a dictionary
 
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


jrpfinch wrote:
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



All times are GMT +1. The time now is 01:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com