Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 -- ""
like image 698
user66001 Avatar asked May 07 '12 01:05

user66001


1 Answers

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
like image 130
HK1 Avatar answered Oct 29 '22 04:10

HK1