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 -- ""
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With