Export (0) Print
Expand All
2 out of 2 rated this helpful - Rate this topic

Configuring Dictionary Properties

Microsoft® Windows® 2000 Scripting Guide

The Dictionary object has only one configurable property, Compare Mode, which plays an important role in determining which keys can be added and which ones cannot. (It is also important in verifying which keys are present in a Dictionary and which ones are not.) By default, a Dictionary is created in binary mode, which means each key in the Dictionary is based on its ASCII value. This is important because the ASCII value of an uppercase letter is different from the ASCII value of that same lowercase letter. In binary mode, both of these services can be added to the Dictionary as individual keys:

  • alerter

  • ALERTER

In other words, with binary mode, you can inadvertently add multiple entries for the same item. Binary mode dictionaries can also be difficult to search. For example, if you try to verify the existence of the key Alerter, you will be told that the key does not exist; that is because no key exists with that same pattern of uppercase and lowercase letters. As a result, you might end up adding yet another key for this same item.

When a Dictionary is configured in text mode, uppercase and lowercase letters are treated identically. This helps eliminate duplicate keys; you cannot add a key for ALERTER if a key for alerter already exists. It is also much easier to verify the existence of a key; searching for either alerter or ALERTer will find the key named Alerter.

To configure the Dictionary mode, create an instance of the Dictionary object and then set the CompareMode property to one of the following values:

  • 0 - Sets the mode to binary. This is the default value.

  • 1 - Sets the mode to text.

For example, the script in Listing 4.44 sets the Dictionary to text mode.

Listing 4.44 Configuring the Dictionary Object

  
1
2
3


Const TextMode = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.CompareMode = TextMode

You cannot change the CompareMode property of a Dictionary if that Dictionary contains any elements. This is because the binary mode allows you to differentiate between keys based solely on uppercase and lowercase letters. For example, each of these keys can be in the same Dictionary as long as the Dictionary is in binary mode:

  • apple

  • Apple

  • APPLE

In text mode, however, these three keys are considered identical. If you had these elements in a binary Dictionary and were able to reconfigure that Dictionary in text mode, the Dictionary would suddenly contain three duplicate keys, and it would fail. If you must reconfigure the Dictionary mode, you first need to remove all items from the Dictionary.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.