Visual Basic® Scripting Edition
|| Language Reference |
See Also Properties Methods
Object that stores data key, item pairs.
A Dictionary object is the equivalent of a PERL associative array. Items, which can be any form of data, are stored in the array using the object.Add key, item method. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually a integer or a string, but can be anything except an array.
The following code illustrates how to create a Dictionary object:Dim d 'Create a variable Set d = CreateObject("Scripting.Dictionary") d.Add "a", "Athens" 'Add some keys and items d.Add "b", "Belgrade" d.Add "c", "Cairo" ...
The Item value will be returned if the object is referenced with the key value 'object.[Item](key)[ = newitem]:Response.Write d("c") Response.Write d.Item("c")
If key is not found when attempting to return an existing item, a new key is created and its corresponding item is left empty. To avoid creating a new key and blank item, use .Exists(key) to check.if d.Exists("f") then Response.Write d("f")
Keys are, by defaut, case sensitive. To use non-case sensitive keys, set the CompareMode object.CompareMode[ = compare] where compare is 0 (Binary), 1 (Text), or 2 (Database).
Keys can be replaced using object.Key(key) = newkey or removed using object.Remove(key) which also removes the Item, of course. object.RemoveAll removes all the Keys and Items from the dictionary.
The For each command can be used to enumerate through the dictionary elements.For each x in d Response.Write "<OPTION VALUE=" & x & ">" & d(x) Next
will produce a list of the keys:<OPTION VALUE=a>Athens <OPTION VALUE=b>Belgrade <OPTION VALUE=c>Cairo
Note that this code works only because of the "<OPTION>" string being pre-pended to the ilterator. The ilterator is an object and must be converted to a string before use. Null keys and other strange values can cause problems which must me managed via on error or TypeName() checking. Another way to ilterate the dictionary is with object.Keys which returns a zero based Array containing all existing keys and object.Count which returns the number of key/item pairs in the object.a = d.Keys 'Get the Keys For i = 0 To d.Count -1 'Iterate the array Print a(i) 'Print Key Print d(a(i)) 'Print Item for that Key Next
It is also possible to ilterate the Items in the dictionary with object.Items which Returns a zero based array containing all existing Items.a = d.Items 'get the Items For i = 0 To d.Count -1 'Iterate the array Print a(i) 'Print Item Next
© 1996 by Microsoft Corporation.
|file: /Techref/language/asp/vbs/vbscript/277.htm, 5KB, , updated: 2007/8/20 17:20, local time: 2018/2/23 10:08,
|©2018 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?|
<A HREF="http://techref.massmind.org/techref/language/asp/vbs/vbscript/277.htm"> Microsoft® Visual Basic® Scripting Edition </A>
|Did you find what you needed?|
Welcome to massmind.org!
Welcome to techref.massmind.org!