Scripting.Dictionary

From StealthBot Wiki
Jump to: navigation, search

A Scripting.Dictionary is a standard object provided by the Microsoft Scriping Runtime (scrrun.dll) dependency. It allows you to store several "item"s with respective "key"s. Each item and key are "linked" together so that you can later use the key to get the item. There is always one item per key, and two keys cannot be the same value.

You can visualize this object as an array where each element has a "key" that can be used to access it that can be anything as opposed to being strictly a numeric index for that element. Literal use of the dictionary as a dictionary illustrates this: the key is a word or phrase and the item is the definition of that word.

Creating

To create a Dictionary, call the CreateObject() function with the string "Scripting.Dictionary" as the name:

Set Dict = CreateObject("Scripting.Dictionary")

The resulting object referenced by Dict will be an empty dictionary object.

Properties

These properties are available from a dictionary object.

CompareMode property

Sets or returns the method that the dictionary object will compare string keys in the Exists() function and the Item property.

You may use the built-in CompareMode constants or the equivalent values enumerated in the Scripting Runtime:

Const BinaryCompare = 0
Const TextCompare = 1
Const DatabaseCompare = 2

You must set this property before adding any key-item pairs, but you can retrieve it at any time.

Count property

Returns the number of key-item pairs currently stored in the dictionary.

Item property

Sets or returns the item for the provided key in the dictionary. If you attempt to retrieve an item with a key that doesn't exist, the result will be an empty value.

You can use this property as an alternative to the Add() function if you do not want to check if the key already exists and you want to overwrite any previous item with that key.

This property is the default property of this object.

You may pass any type of value, including arrays and objects (using the Set keyword), as the item in a dictionary (or even the key, but items with complex keys are harder to retrieve).

If the key is a string, retrieval/rewriting of the item is based on the CompareMode.

Examples:

Item = Dict.Item(Key)
Item = Dict(Key)
Dict.Item(Key) = Item
Dict(Key) = Item

Key property

Sets the value of the provided key to a different value. You cannot retrieve a value from this property.

This property can be used to "rename" an item.

Example:

Dict.Key(OldName) = NewName

Methods

These methods are available from a dictionary object.

Add function

Adds a key-item pair to a dictionary. The provided key must not already exist when this function is called. Use the Exists() function on the key if you want to check.

Dict.Add Key, Item

You may pass any type of value, including arrays and objects, as the item in a dictionary (or even the key, but items with complex keys are harder to retrieve).

If the key is a string, checking for duplicate keys is based on the CompareMode.

Exists function

Returns a Boolean whether the specified key exists in the dictionary.

BoolValue = Dict.Exists(Key)

If the key is a string, checking whether the key exists is based on the CompareMode.

Items function

Returns all of the items in the dictionary as an array. This can be used to iterate through the items in the dictionary using a For loop, for example.

ArrItems = Dict.Items()

Keys function

Returns all of the keys in the dictionary as an array. This can be used to iterate through the keys in the dictionary using a For loop, for example.

ArrKeys = Dict.Keys()

Remove function

Removes the key-item pair from the dictionary by the provided key.

Dict.Remove Key

If the key is a string, searching is based on the CompareMode.

RemoveAll function

Removes all key-item pairs from the dictionary.

Dict.RemoveAll

See also