user66001
user66001

Reputation: 935

VBScript Dictionary Exists Method Always Returns True

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

Upvotes: 8

Views: 34332

Answers (6)

Javier Guzman
Javier Guzman

Reputation: 9

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

Upvotes: 0

Cassiopeia
Cassiopeia

Reputation: 313

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.

Upvotes: 0

Juliano CWB
Juliano CWB

Reputation: 9

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.

Upvotes: 0

user3429488
user3429488

Reputation: 1

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

Upvotes: 0

Jon
Jon

Reputation: 5

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

Upvotes: -2

HK1
HK1

Reputation: 12220

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

Upvotes: 14

Related Questions