it seems that if you test the value of a dictionary
object item in an if statement, and the key doesn't
exist, it is created in the dict with an empty value..

we got around this by doing a .exists before the if
statement, however this still seems a little strange

eg.

set oDict = CreateObject("Scripting.Dictionary")
oDict("key1") = "Y"
oDict("key2") = "N"
oDict("key3") = "Y"

' this results in the expected keys and values
for each key in oDict
response.write key & ":" & oDict(key) & "<br>"
next

sInvalidKey = "key4"

' refer to a key that doesn't exist
if oDict(sInvalidKey) = "Y" then
' whatever
end if

' this results in the expected keys and values
' with the addition of key4 with an empty value!
for each key in oDict
response.write key & ":" & oDict(key) & "<br>"
next

Re: is this a bug in 5.6 by Michael

Michael
Thu Oct 23 22:57:49 CDT 2003

av wrote:
> it seems that if you test the value of a dictionary
> object item in an if statement, and the key doesn't
> exist, it is created in the dict with an empty value..
>
> we got around this by doing a .exists before the if
> statement, however this still seems a little strange


This is actually documented behavior...

if oDict(sInvalidKey) = "Y" then

is the equivalent of

if oDict.Item(sInvalidKey) = "Y" then

where you are attempting to retrieve the value of an item with the given key
in sInvalidKey.

Item Property
http://msdn.microsoft.com/library/en-us/script56/html/jsproitem.asp

See the Remarks section which states:

"... If key is not found when attempting to return an existing item, a new
key is created and its corresponding item is left empty. ..."


--
Michael Harris
Microsoft.MVP.Scripting

Windows 2000 Scripting Guide
Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp

TechNet Script Center Sample Scripts
http://www.microsoft.com/downloads/release.asp?ReleaseID=38942

WSH 5.6 documentation download
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en