Introduction
First and foremost I wish to thank two of my team members, Yasmin Helled and Eyas Kopty, who actually brought this issue to my attention. This post actually replaces an earlier post in which I prematurely and erroneously declared the issue as being a bug. My apologies to you, my loyal readers, for falling with such pitfall. I hope this short article will enable you avoid another one, even more substantial - one that can impact the way your code behaves.
Discussion
Yasmin had showed me a piece of code similar to the following (I simplified it a bit for illustration purposes):
If Not (objDic.Exists("MyKey") Or IsNumeric(objDic("MyKey"))) Then MsgBox "Key Not Found" Else MsgBox "Key Found: " & objDic("MyKey") End If
The key, needless to say, was not previously added, but the condition consistently entered the Else clause, printing an Empty value in the MsgBox. This sounded weird to Yasmin, who had forgotten that VBScript always checks ALL conditional clauses, so that even though the first part of the condition already returned true, the second part is also checked. This resulted in runtime problems, since the key thus added contained an empty value!
To overcome this problem, the above code was replaced with the following:
'Declare variables Dim objDic, strKey 'Create instance of Dictionary Set objDic = CreateObject("Scripting.Dictionary") 'Assign value to required key strKey = "MyKey" If Not (objDic.Exists(strKey)) Then 'Check if the key exists MsgBox "Key " & strKey & " was not found." ElseIf Not IsNumeric(objDic(strKey)) Then 'The key exists so check if it’s numeric MsgBox "Value is not numeric: " & objDic(strKey) Else 'Report that the validation succeeded MsgBox "Key " & strKey & " was found and the value is " & objDic(strKey) End If
So that functional independence between key existence and value type validation is achieved.
Conclusion
When addressing a non-existing key in a Dictionary object, the add method is invoked by default. The problem is that the Windows Script Host does not inform during runtime that there is a missing key. Moreover, the automatically assigned Empty value may cause trouble to our subsequent code execution. I hope that bringing up this issue will benefit programmers and enable them to avoid such a pitfall.
*Note: This revised post replaces a previous post titled "Amazing Bug in the Dictionary Object!"
Posted in Dictionary Objects, Meir Bar-Tal's Blog

Meir Bar-Tal





June 25th, 2008 at 4:56 pm
Have you considered adding methods/functions to allow for iteration through the Dictionary object? For example “Inc”, “DeInc”, “Set_Iterator”, “Reset_Iterator”, “BOF”, and “EOF”? That way a developer can loop though a dictionary accessing all its items.
June 30th, 2008 at 8:27 am
Great post! I have been thinking for years there was something wrong with my logic of the first OR comparison and there was some data cleaning needed. In fact, I had developed a basic best practice in QTP: Never test more than one condition at a time in an IF statement.
Maybe this is more of a pitfall for old WinRunner (and current C) programmers? That’s where most of us got the notion that the 2nd half of a failed OR statement never executes, right?
July 1st, 2008 at 10:08 am
VBS doesn’t demand to declare the parameter mandatorily before using it. It may not be a problem, I think.