1

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?

2
  • 1
    Are you trying to do this in VB.NET? or VBA? Your tag says VBA. Commented May 26, 2020 at 2:14
  • @braX excel vba Commented May 26, 2020 at 2:17

2 Answers 2

1

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 

enter image description here

Use this as a reference

“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!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your brief explain. However, how can I assign a value to b(3) ? Coz my case is when I do oDict(5)(3) = 10, b(3) still return 4
Ok, there is your code working. You need to take the array from the dict, Check this answer: Link
AND! if this helps you, please... set the answer! Send some love please!
Just change some comments in the correction of your code.
0

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"} 

6 Comments

Thanks for your reply, but how can I add array as value in dictionary?
Note that that's the .Net link (the tag is VBA, not Vb.net).
This can be found here vb.net-informations.com/collections/dictionary.htm Dim dict As New Dictionary(Of KeyType, ValType)() In practice, it would be: Dim compAndCEO As New Dictionary(Of String, String)() compAndCEO.add("Amazon", "Jeff Bezos")
@WILLIAM If this solves your question, could you please mark my response as the answer?
This does not work for VBA. VBA is not the same thing as VB6 or VB.NET
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.