Reputation: 485
I have a dictionary
and which also storing array
. Here is the code and I am trying to assign some value to the array. But it always fails to assign value to the array
. How can I solve this? Thanks
Code:
Dim dict As Dictionary
Set dict = New Dictionary
For i = 1 To fruitList.count
dict .Add fruitList(i), Array(0, 0, 0, 0, 0)
next
dict(apple)(0) = 100 //however, the first index in the array always equals to 0
Update:
Or I should ask in this way. Isn't the way I add array
as value in dictionary
wrong?
Upvotes: 1
Views: 1212
Reputation: 1716
Give this a try:
Sub addArray2Dict()
Dim a: a = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) 'Here declare and store the array,
'you can do it any other way
'there is no difference if you do it here or
'in the loop
Dim b 'Just for testing porpuse
Dim oDict As Object 'Because I use “Microsoft Scripting Runtime” can use earle binding
Set oDict = CreateObject("Scripting.Dictionary")
Dim i 'iterator
'Store numbers and letters inside the dictionary
'BUT in the index number 5 I will store the array
For i = 1 To 20
If i = 5 Then
oDict.Add i, a
'oDict.Add i, Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) 'As I said, this is the same as the line above.
Else
oDict.Add i, Chr(66 + i) 'Storing just letters in every key(integer)
End If
Next i
b = oDict(5) 'Here I take the array from the Dictionary, and store it inside b,
Debug.Print b(3) 'Here just print the 3th (number 4) elemente of the array
'remember the array is:
'
' array( 1 2 3 4 5 6 7 8 9 0 ) right!
' 0 1 2 3 4 5 6 7 8 9 <<< with that index
'
' if i want the 3th value i will get the number 4 in my example.
End Sub
“Microsoft Scripting Runtime” (Add using Tools->References from the VB menu)
As I understand your code could be this:
Sub Test()
Dim fruitList(1 To 5) As String
Dim i
Dim B
fruitList(1) = "apple"
fruitList(2) = "banana"
fruitList(3) = "melon"
fruitList(4) = "orange" 'Love oranges!
fruitList(5) = "kiwi" 'Love Kiwi too!!!!
Dim dict As Dictionary
Set dict = New Dictionary
For i = LBound(fruitList) To UBound(fruitList) 'here you use the bounds of the array,
'instead .count, here won't work
dict.Add fruitList(i), Array(0, 0, 0, 0, 0)
Next
B = dict("apple") '(0) = 100 'however, the first index in the array always equals to 0
B(0) = 100 'Here! You take the array FROM the dictionary and store it inside B
'Just do it!
B(1) = 200
B(2) = 300
B(3) = 500
B(4) = 1000000
Debug.Print B(0) 'Testing! This will print in the console your value (100).
dict.Remove "apple" 'Remove the array
dict.Add "apple", B 'Return the new array!
End Sub
You can't change the values of an array stored inside an Dictionary (in VBA). It is better to take away de array and change the values, and after that, if you need it to, stored back in the dict.
Check this answer If Tim Williams says you can't it is because nobody can!
Upvotes: 1
Reputation: 596
This can all be found here: https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/
Dim myArray(arraySize) As ArrayType
or
Dim myArray = New ArrayType() {Item1, Item2, ...}
So, in practice, it should look like this:
Dim companies(3) as String
companies(0) = "Microsoft"
companies(1) = "Google"
companies(2) = "Amazon"
or:
Dim companies = New String() {"Microsoft", "Google", "Amazon"}
Upvotes: 0