Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


May 19, 2000

Understanding VBScript: The Dictionary Object—An Alternative to Arrays


RSS
View this exclusive article with VIP access -- click here to join |
See More VBScript Articles Here | Reprints | Or sign up for our VIP Monthly Pass!

Arrays are the first construct that VBScript instructors introduce when they discuss how to group data. With arrays, you can store columns of data in one place, then access the data later through one variable. However, years of real-world use have revealed that arrays aren't always the most desirable solution to gather and maintain related data. Fortunately, a new type of array has emerged: the dictionary. Here's a look at what dictionaries are and how you manipulate them with the methods and properties that Table 1 shows.

Comparing Dictionaries and Arrays
A dictionary is a general-purpose data structure that looks like a linked list but acts like a "super array." Like VBScript arrays, dictionaries store data and make that data available through one variable. (If you're unfamiliar with VBScript arrays, see my July 1999 column.) However, dictionaries differ from arrays in many ways, including

  • A dictionary has additional methods to add new items and check for existing items.
  • You don't need to call ReDim to extend the dictionary's size.
  • When you delete a particular item from a dictionary, all the subsequent items automatically shift up. For example, if you delete the second item in a three-item dictionary, the original third item automatically shifts up into the second-item slot.
  • You use keys to identify dictionary items. Keys can be any data subtype, except an array or dictionary.
  • A dictionary can't be multidimensional. (Although you can't store arrays or dictionaries in a dictionary item, you can store dictionaries in array items.)

The most important reason for using a dictionary instead of an array is that a dictionary is more flexible and is richer in terms of built-in functionality. Dictionaries work better than arrays when you need to access random elements frequently. Dictionaries also work better when you want to locate items by their content rather than their position. . . .


Already a VIP member?
Please log on to view the full article

Why become a VIP member?

VIP-only online access
VIP CD delivered twice a year: offline access to the entire Windows IT Pro article library
Monthly issue of your choice of Windows IT Pro or SQL Server Magazine

Subscribe Now
Reader Comments
<p>I just started reading this article and noticed a mistake. The article states: "A dictionary can't be multidimensional. (Although you can't store arrays or dictionaries in a dictionary item, you can store dictionaries in array items.)" I'm not sure if this is due to a change in the Dictionary object over time, but you can certainly do this with the one I have. Here is the sample code to show adding Dictionaries to a Dictionary and adding an array (that happens to be multidimensional itself) to a Dictionary:<br>
<br>
Dim d1, d2 As Dictionary<br>
Dim array1(3, 3) As String<br>
Dim x, y As Long<br>
Set d1 = New Dictionary<br>
For x = 1 To 3<br>
Set d2 = New Dictionary<br>
d2.Add CStr(x), "value" & CStr(x)<br>
d1.Add CStr(x), d2<br>
Next x<br>
Debug.Print d1("1")("1")
For x = 1 To 3<br>
For y = 1 To 3
array1(x, y) = CStr(x) & " " & CStr(y)<br>
Next y<br>
Next x<br>
For x = 4 To 6<br>
d1.Add CStr(x), array1<br>
Next x<br>
Debug.Print d1("6")(3, 3)<br>
Set d2 = Nothing<br>
Set d1 = Nothing<br>
Erase array1<br>
<br>
The output is:<br>
value1<br>
3 3</p>

David Doucette August 04, 2002


I had trouble finding out how to adress an element in an array as the value of a dictionary. You solved it!

Anonymous User January 31, 2005 (Article Rating: )


Great article for understanding the power of dictionaries!

Anonymous User April 06, 2005 (Article Rating: )


very useful article which helped me understand the Dictionary Object in Vbscript

Anonymous User July 05, 2005 (Article Rating: )


A good article, but I wanted more - I wanted multidimensional array type capability and it can be done with a dictionary object. Here's the syntax for adding an item:-

objDictionary.Add "New Year's Day - Firm Holiday", Array("2007/1/1", "All Offices")

What stumped me for a little bit was how to extract and reference the array, but this works:-

msgbox strKey & " - " & DateValue(objDictionary.Item(strKey)(0)) & " ~ " & objDictionary.Item(strKey)(1)

handl@rcn.com January 24, 2007 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Windows 7 Sets Sales Record

Microsoft CEO Steve Ballmer described Windows 7's first ten days of sales as "fantastic" while in Japan yesterday. ...


Related Articles Don't Let Your AD Scripts Hang on You

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement