When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Wednesday, October 28, 1998

WebDaily: Your daily source for Web Technology Tips and Tricks!


*****************************************************************
Utilizing the Dictionary Object in VBScript
*****************************************************************

* This article discusses using the Dictionary object in your ASP pages

One of Perl's nice features is what is called an associative array. An associative array differs from a "normal" array in one major way: rather than being indexed numerically (i.e. 0, 1, 2, 3, ...), it is indexed by a key, or an English-like word. ASP has something very similar to an associative array. This object is called the Dictionary object.

To construct an instance of a dictionary object, just use the following lines of code:

Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")

It's that simple. To add items to your dictionary object, all you need to do is use the Add method. The Add method takes two parameters, the key and the item. The key is similar to the index in a numerically-based, indexed array, and the item is the value at that index. Here is an example of using the add method:

objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"

Note that "Name" and "Age" are the keys and "Scott" and "20" are the items corresponding to those keys. To see if a key exists, just use the Exists method:

if objDictionary.Exists("Name") then
' Do something
else
' Do something else

end if

You can retrieve all of the items and keys using one of two methods. The Items method returns an array of all the items in the Dictionary object, and the Keys method returns an array of all the Keys in the Dictionary object.

Dim ItemsArray, KeysArray
ItemsArray = objDictionary.Items
KeysArray = objDictionary.Keys

' The # of Items = UBound(ItemsArray)
' You can traverse through the array to print out all the values


To get a specific item, use the Item property.
Dim MyName
MyName = objDictionary.Item("Name")
' MyName = "Scott"


You can use the Count property to get the number of keys / items in a Dictionary object (objDictionary.Count).

The last important methods are the Remove and RemoveAll methods. RemoveAll removes all of the key / index pairs, while the Remove takes a Key as a parameter and removes the key/item pair.

objDictionary.Remove("Name")

' Now the "Name" / "Scott" are no more

objDictionary.RemoveAll

' Now the entire Dictionary object is empty


The Dictionary object is like Perl's associative array. It is not there to replace the array, but there are certainly times when it makes more sense to index your array using English-like terms as opposed to numerical values. Suppose, for instance, that you wanted to keep track of the current user's name, their referring URL, and their IP. You could make an instance of a Dictionary at the Session level in Session_OnStart, and populate it with the data from the Request.ServerVariables object. Then you could easily refer to your web visitor's information by objDictionary.Item("IP") or objDictionary.Item("RefURL") rather than using an array or three separate Session variables.

(For more information on the Request.ServerVariables object, please direct your browser to my article Using the Request.ServerVariables Object.)

Happy Programming!


*****************************************************************
*****************************************************************
To subscribe to WebDaily, point your browser to:
http://www.4GuysFromRolla.com/webtech/webdaily
*****************************************************************
*****************************************************************

Please forward this information to a friend, including the subscription URL! Happy Programming!


Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES