1

This code was working for several days, looping through a dictionary just fine. The loop started breaking, and I have extra keys in the dictionary and I have no idea where they came from.

I am creating a scripting.dictionary with the following:

Dim riskDict As New Scripting.Dictionary

After which, the object is empty, as seen in the watch:

enter image description here

I then add my first key/item pair:

riskDict.Add "Weight", Array("WP", 0)

And after running this one line of code, I now have this:

enter image description here

Where did these two extra keys (Item 2 and 3) come from?! This is a problem, since later in my code I use:

For Each key In riskDict
    temp = riskDict(key)
    ...
Next key

And this loop breaks, since it starts referring to keys which are empty. This does not seem to have been happening until just now, and the code has been running fine for a few days. (I changed some things elsewhere in the code, but completely unrelated to this.)

Let me know if I am doing anything ridiculous, or missing something obvious, and thanks!

lukehawk
  • 1,423
  • 3
  • 22
  • 48
  • You have got two different variables `riskDict` and `riskGroupDict`, is that right? It's not clear since you didn't share the whole code. – omegastripes Dec 29 '15 at 19:41
  • Youre right, but i simply grabbed the wrong chunk of code. I fixed my question, but thanks for the heads up! – lukehawk Dec 29 '15 at 19:43
  • Could you please post the whole `For Each` loop code. Note that just referencing to an item of a dictionary adds new element if it isn't present, without invoking `Add` method. – omegastripes Dec 29 '15 at 19:47
  • I can, but i do not feel like it has anything to do with the problem. The problem happens BEFORE the loop ever starts. It will be alittle bit before i can get back to my desk. – lukehawk Dec 29 '15 at 19:51
  • Is `riskDict` a global variable? – John Coleman Dec 29 '15 at 21:18

2 Answers2

0

Make riskDict private or clear your array before using it. Hard to tell what your doing with it since there is critical elements missing.

Dim riskDict As New Scripting.Dictionary
Nightforce2
  • 1,426
  • 5
  • 18
  • 31
0

Although this is an old question, it might still be of interest to other users.

Before the first item has been added to an initialized dictionary object, the local window of the VBA editor must not be switched on if a breakpoint exists in the scope of this Dictionary object. Otherwise a key-value pair (0, Empty) might be inserted into the dictionary. This dictionary entry cannot be deleted again in the immediate window, even Dictionary.RemoveAll fails. This is the case even if the local window has been closed again in the meantime.

The exact circumstances under which this behavior occurs are not clear to me: I've only observed this behavior of Dictionary objects in class modules so far, but on the same line of code it sometimes occurs, sometimes not.

This SO discussion also describes the behavior when a watch is set on the Dictionary object. There, this behavior is associated with the property of dictionaries that when a query is made for an unknown key, this key is automatically added with a value of 'Empty'.

Olli
  • 327
  • 2
  • 9