8

What am I doing wrong? From my tests, objDic.exists NEVER gives False!

    dim objDic

    set objDic = createobject("scripting.dictionary")

    objDic.add "test","I have not been deleted"

    wscript.echo objDic.item("test") 'Displays -- I have not been deleted

    objDic.remove "test"

    wscript.echo """" & objDic.item("test") & """" 'Displays -- ""

    if objDic.exists("test") then wscript.echo """" & objDic.item("test") & """" 'Displays -- ""
HK1
  • 11,941
  • 14
  • 64
  • 99
user66001
  • 774
  • 1
  • 13
  • 36

6 Answers6

13

As near as I can tell, a Dictionary Object Key is created by merely referencing it as though it does exist.

wscript.echo objDic.Item("test") 'Creates the key whether it exists or not
wscript.echo objDic.Exists("test") 'Will now return true

Here's some more code you can try to prove/test my theory. I usually use MsgBox instead of WScript.Echo, as you will see in my code.

dim objDic, brk
brk = vbcrlf & vbcrlf
set objDic = createobject("scripting.dictionary")
objDic.add "test","I have not been deleted"
wscript.echo "objDic.Exists(""test""): " & brk & objDic.item("test")
WScript.Echo "Now going to Remove the key named: test"
objDic.remove "test"
MsgBox "objDic.Exists(""test""): " & brk & objDic.Exists("test") 'Returns False
wscript.echo "objDic.item(""test""): " & brk & objDic.item("test") 'Shows Blank, Creates the key again with a blank value
wscript.echo "objDic.item(""NeverAdded""): " & brk & objDic.item("NeverAdded") 'Also shows blank, does not return an error
MsgBox "objDic.Exists(""test""): " & brk & objDic.Exists("test") 'Returns True
HK1
  • 11,941
  • 14
  • 64
  • 99
  • 2
    Absolutely correct. Here's the [official documentation](http://msdn.microsoft.com/en-us/library/84k9x471%28VS.84%29.aspx), which mentions, "If *key* is not found when attempting to return an existing item, a new *key* is created and its corresponding item is left empty." – Cheran Shunmugavel May 07 '12 at 04:57
  • 2
    Prompted by the new answer, in revisiting this question, I am as similarily confused as to how creation of a key if it doesn't exist is helpful, as I was when I last looked at this question. Can anyone explain reasons behind this? I can only assume that this is rooted in comp. sci. theories about dictionary objects?? – user66001 Jan 18 '13 at 21:48
  • 4
    @user66001 As a pragmatic programmer, I worry less about why something like this is designed the way it is and worry more about remembering these kinds of quirks. – HK1 Jan 19 '13 at 02:59
0

Delete all watched variables from the IDE that have anything to do with your dictionary. It is repeatable. You can cause/fix the behavior this way (Outlook 2010 VBA IDE). Sort of like the observer effect I guess . . .

-M

  • Alas I was coding in Notepad, so (to my understanding) watched variables wouldn't be the cause of my issue. – user66001 May 01 '21 at 15:34
0

Had the same problem here...
In my code, there's an dynamic array of dictionaries.
Some of the entries have the key "HIGH", some doesn't.
Testing for the presence of the key for each entry always returned true:

for each dictionary_entry in dictionary_array
      if dictionary_entry.Exists("HIGH") then msgbox("Hey, I have a HIGH key, and its value is " + dictionary_entry("HIGH))
next

The debugger created a "HIGH" key for every dictionary_entry if I watched the variable.
Had to delete both dictionary_entry and dictionary_array from watched variables to get the code working right.

  • -1 https://stackoverflow.com/questions/10475669/vbscript-dictionary-exists-method-always-returns-true/#22458455 appears to be a duplicate of your answer. Also, alas I was coding in Notepad, so (to my understanding) watched variables wouldn't be the cause of my issue. – user66001 May 01 '21 at 15:40
0

This problem was causing me a lot of trouble.

Turns out I was running an "If" function on the key earlier in the code and it was therefore adding it to the dictionary.

If dict(key) = "chocolate" then 
Check = dict.exists(key) 'Always resolves to true
End If
Whatis = dict(key) 'resolves to ""

So the value from the if function is not added to the dictionary, but the key is added.

Cassiopeia
  • 313
  • 1
  • 4
  • 16
  • My example defined the variable with no other if statements between that definition and the .exists check, so while this may be the cause of other's version of this issue, it couldn't have been mine. – user66001 May 01 '21 at 15:38
0

Had the same problem, deleted all the watched variables in the IDE and it fixed it

  • -1 https://stackoverflow.com/questions/10475669/vbscript-dictionary-exists-method-always-returns-true/#22458455 & https://stackoverflow.com/questions/10475669/vbscript-dictionary-exists-method-always-returns-true/#32998455 appear to be a duplicates of your answer. Also, alas I was coding in Notepad, so (to my understanding) watched variables wouldn't be the cause of my issue. – user66001 May 01 '21 at 15:38
-2

The accepted answer did not answer my question. I imagine others either so I'm posting my solution since this thread is the first result in google.

The key will be created by default if it does not exists. Dictionaries are meant to add entries if they do not exist, hence the below will always return true.

If objDic.exists("test") then

Since the key is created when you test for it's existence, the value has not been defined. The below will test if the Key does not have a value associated with it. Of course this will not work with your dictionary if you have blank values.

If objDic.item("test") <> "" then
user66001
  • 774
  • 1
  • 13
  • 36
Jon
  • 5
  • 1
  • 4
    this is wrong. An attempt to read/access a non-existent key will create an empty value (auto-vivification), but .Exists("nix") will return false, if the dictionary does not contain "nix". see: http://stackoverflow.com/questions/6910996/scripting-dictionary-lookup-add-if-not-present-with-only-one-key-search – Ekkehard.Horner Jan 18 '13 at 21:48
  • 1
    Unfortunately @Jon I cannot replicate your results on XP or Win 7 [in my tests](http://i.imgur.com/6FqAq.png) either – user66001 Jul 15 '13 at 17:20