"Dictionary" Objects to Store Keys and Values

This section describes Dictionary object properties and methods. A tutorial example is provided on how to add and remove keys and values into a Dictionary object.

Dictionary: An object representing a collection of pairs of keys and values, working like an associate array in Perl. It offers the following methods and properties:

Here is a VBScript example to show you how to use "Dictionary" objects:

<html>
<body>
<!-- dictionary_test.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
   ' Creating a dictionary object
   set user = CreateObject("Scripting.Dictionary")
   ' Adding a pair of key and item
   user.add "FirstName", "Bill"
   user.add "LastName", "Smith"
   user.add "Email", "bill@com.com"
   user.add "Country", "Canada"
   
   ' Iterating through the dictionary
   n = user.Count
   document.writeln("Before - Number of keys: " & n)
   keys = user.Keys()
   for i=0 to n-1
      k = keys(i)
      v = user.Item(k)
      document.writeln(k & ": " & v)
   next
   
   ' Modifying the dictionary
   user.remove("Country")
   user.Item("Email") = "bill@smith.com"
   user.Item("Phone") = "123-456-7890"
   
   ' Iterating through the dictionary
   document.writeln("After - Number of keys: " & n)
   keys = user.Keys()
   for i=0 to n-1
      k = keys(i)
      v = user.Item(k)
      document.writeln(k & ": " & v)
   next
</script>
</pre>
</body>
</html>

Output:

Before - Number of keys: 4
FirstName: Bill
LastName: Smith
Email: bill@com.com
Country: Canada
After - Number of keys: 4
FirstName: Bill
LastName: Smith
Email: bill@smith.com
Phone: 123-456-7890

Note that:

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

scrrun.dll - Scripting Runtime DLL Library

 Scripting Runtime DLL Library Overview

"Dictionary" Objects to Store Keys and Values

 "FileSystemObject" Objects to Manage File Systems

 "Drive" Objects Representing Disk Drives

 "Folder" Objects Representing File Folders

 "File" Objects Representing Files

 "TextStream" Objects Representing File Input and Output

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 PDF Printing Version