Login   /   Register

A Pitfall for Beginners when Working with a Dictionary

Rate this article
     1 votes, average: 5 out of 51 votes, average: 5 out of 51 votes, average: 5 out of 51 votes, average: 5 out of 51 votes, average: 5 out of 5
Loading ... Loading ...
April 23rd, 2008 by Meir Bar-Tal

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

3 Responses to “A Pitfall for Beginners when Working with a Dictionary”

  1. stadlerc Says:

    [+]

    Have you considered adding methods/functions to allow for iteration through the Dictionary object? For example "Inc", "DeInc", "S... ...

  2. pmgrossman Says:

    [+]

    Great post! I have been thinking for years there was something wrong with my logic of the first OR comparison and there was some d... ...

  3. heqingbluesky Says:

    [-]

    VBS doesn’t demand to declare the parameter mandatorily before using it. It may not be a problem, I think.

Leave a Reply

You must be logged in to post a comment.

This article was viewed 621 times