Some context
This dictionary describe specific eateries at different blocks. The nested dictionaries are the food elements and the overall rating for them
ckDict looks like:
[
"808": [
"Carbohydrate Texture": 6,
"Carbohydrate Quantity": 3
],
"826": [
"Carbohydrate Texture": 6,
"Carbohydrate Quantity": 3
]
]
In the context of this question, ckBlk is constant as it is part of a parent loop.
The problem
In the main loop, I printing ckBlk for every iteration, it does not change.
After finishing with the debug loop, I end my whole program. (for debugging)
This means ckBlk does not get changed during runtime.
However, when I print from debug loop, you can the values from the first ckblk I print overflows into all other ckblks ( I initialized ckDict to have all the blocks and questions already)
I even tried replacing ckBlk in setArr(arrCounter).ckDict(ckBlk)(fdCat_qnType) = CInt(Left(fdVotes(1, fdVotesCounter), 1) to "808" which is my first ckBlk and others are still affected (testing with block "826")
808 and 826 are blocks, with the question inside questions and the relevant rating
setArr is an array of ckDicts
Code
For Each fdCat_qnType In setArr(arrCounter).ckDict(ckBlk)
Debug.Print ("ckBlk: " & ckBlk & " | Qn: " & fdCat_qnType & " | Value: " & CInt(Left(fdVotes(1, fdVotesCounter), 1))) ' IMPORTANT FOR DEBUG
Debug.Print ("826 before insertion: " & setArr(arrCounter).ckDict("826")(fdCat_qnType))
setArr(arrCounter).ckDict(ckBlk)(fdCat_qnType) = CInt(Left(fdVotes(1, fdVotesCounter), 1))
Debug.Print ("826 after insertion: " & setArr(arrCounter).ckDict("826")(fdCat_qnType))
Debug.Print ("808 after insertion: " & setArr(arrCounter).ckDict("808")(fdCat_qnType))
Debug.Print ("")
fdVotesCounter = fdVotesCounter + 1
Next fdCat_qnType
Output
ckBlk: 808 | Qn: Main Protein Taste | Value: 1
826 before insertion: 0
826 after insertion: 1
808 after insertion: 1
Found out the problem! Because my nested dictionaries are generated dynamically, when doing e.g.
dict[parentDict][subdictX] = subdictit passes the reference pointer to subdictX instead of the actual value. So this means that the solution is to re-set the subdictSet subdict = CreateObject("Scripting.Dictionary")whenever I declare subdict to be the value of subdictX