Using Dictionary Object To Address Array Elements With String Indexes

Applies to TestComplete 15.63, last modified on April 23, 2024

It may be easier for you to address the elements of the array using indexes whose type are different from the integer type. Standard arrays only have integer type indexes and cannot help with this task. The solution is to apply the Dictionary object that is provided by the Microsoft Scripting Runtime Library (Scrrun.dll).

A Dictionary object contains a set of key-item pairs. A dictionary’s item is a value that is unambiguously associated with a unique key and the key is used to retrieve the item. The key can be of any type except a variant or an array (but generally it is a string or still an integer). The item can be of any type: integer, real, string, variant, object and so on. You cannot use string indexes in arrays, but you can apply a Dictionary object in its place, and use string keys to access the dictionary items.

The dictionary object has the following benefits when compared with arrays:

  • The size of the Dictionary object can be set dynamically.
  • Dictionaries surpass arrays in locating items by their content.
  • Dictionaries work better than arrays when accessing random elements frequently.
  • The Dictionary object has built-in methods and properties that allow users to manage the dictionary’s contents and keys.
  • When deleting an item from a Dictionary, the remaining items are automatically shifted up.

The only disadvantage of dictionaries when compared to arrays, is that they cannot be multidimensional.

The tables below list the methods and properties of the Dictionary object:

Method Description
Add Adds the key-item pair to the object.
Exists Verifies whether the element with the specified key exists.
Items Returns an array that only holds the dictionary’s values without the associated keys.
Keys Returns an array that only holds the dictionary’s keys without the associated values.
Remove Removes the dictionary’s element specified by a key.
RemoveAll Removes all elements from the dictionary.
Property Description
Count Returns the current number of elements in the dictionary.
Item Returns or sets the dictionary’s item that is associated with the specified key.
Key Defines a new key-value to the specified key.
CompareMode Specifies the mode for comparing string keys. The possible values are: 0 - binary (default), 1 - text and 2 - database.

For detailed information, see the documentation of the Dictionary object and its properties and methods in the MSDN library.

We create the Dictionary object like any other COM object.

The following code samples demonstrate how to use the Dictionary object to address the stored item with the string key.

JavaScript

function DictionaryDemo()
{
  let d = getActiveXObject("Scripting.Dictionary");

  // Add keys and items.
  d.Add("a", "Alphabet");
  d.Add("b", "Book");
  d.Add("c", "Coffee");

  // Get the item by key.
  let s = d.Item("c");
  Log.Message(s);

  // Assign a new item to the same key.
  d.$set("Item", "c", "Cookie");
  s = d.Item("c");
  Log.Message(s);

  // Assign a new key to the same item.
  d.$set("Key", "c", "Co");

  // Remove second pair.
  d.Remove("b");
  if (d.Exists("b"))
      Log.Message("The pair exists.")
    else
      Log.Message("The pair does not exist.")
}

JScript

function DictionaryDemo()
{
  var d = new ActiveXObject("Scripting.Dictionary");

  // Add keys and items.
  d.Add("a", "Alphabet");
  d.Add("b", "Book");
  d.Add("c", "Coffee");

  // Get the item by key.
  var s = d.Item("c");
  Log.Message(s);

  // Assign a new item to the same key.
  d.Item("c") = "Cookie";
  s = d.Item("c");
  Log.Message(s);

  // Assign a new key to the same item.
  d.Key("c") = "Co";

  // Remove second pair.
  d.Remove("b");
  if (d.Exists("b"))
      Log.Message("The pair exists.")
    else
      Log.Message("The pair does not exist.")
}

Python

def DictionaryDemo():
  d = Sys.OleObject["Scripting.Dictionary"]

  # Add keys and items.
  d.Add("a", "Alphabet");
  d.Add("b", "Book");
  d.Add("c", "Coffee");

  # Get the item by key.
  s = d.Item["c"];
  Log.Message(s);

  # Assign a new item to the same key.
  d.Item["c"] = "Cookie";
  s = d.Item["c"]; 
  Log.Message(s);

  # Assign a new key to the same item.
  d.Key["c"] = "Co";

  # Remove second pair.
  d.Remove("b");
  if d.Exists("b"):
    Log.Message("The pair exists.") 
  else:
    Log.Message("The pair does not exist.")

VBScript

Sub DictionaryDemo
  Dim d
  Set d = CreateObject("Scripting.Dictionary")

  ' Add keys and items.
  d.Add "a", "Alphabet"
  d.Add "b", "Book"
  d.Add "c", "Coffee"

  ' Get the item by key.
  s = d.Item("c")
  Log.Message(s)

  ' Assign a new item to the same key.
  d.Item ("c") = "Cookie"
  s = d.Item("c")
  Log.Message(s)

  ' Assign a new key to the same item.
  d.Key("c")="Co"

  ' Remove second pair.
  d.Remove("b")
  If d.Exists("b") Then
      Log.Message("The pair exists.")
    Else
      Log.Message("The pair does not exist.")
  End If
End Sub

DelphiScript

function DictionaryDemo;
var d, s: OleVariant;
begin
  d := Sys.OleObject('Scripting.Dictionary');

  // Add keys and items.
  d.Add('a', 'Alphabet');
  d.Add('b', 'Book');
  d.Add('c', 'Coffee');

  // Get the item by key.
  s := d.Item('c');
  Log.Message(s);

  // Assign a new item to the same key.
  d.Item('c') := 'Cookie';
  s := d.Item('c');
  Log.Message(s);

  // Assign a new key to the same item.
  d.Key('c') := 'Co';

  // Remove second pair.
  d.Remove('b');
  if d.Exists('b') then
      Log.Message('The pair exists.')
    else
      Log.Message('The pair does not exist.')
end;

C++Script, C#Script

function DictionaryDemo()
{
  var d = new ActiveXObject("Scripting.Dictionary");

  // Add keys and items.
  d["Add"]("a", "Alphabet");
  d["Add"]("b", "Book");
  d["Add"]("c", "Coffee");

  // Get the item by key.
  var s = d["Item"]("c");
  Log["Message"](s);

  // Assign a new item to the same key.
  d["Item"]("c") = "Cookie";
  s = d["Item"]("c");
  Log["Message"](s);
  
  // Assign a new key to the same item.
  d["Key"]("c") = "Co";

  // Remove second pair.
  d["Remove"]("b");
  if (d["Exists"]("b"))
      Log["Message"]("The pair exists.")
    else
      Log["Message"]("The pair does not exist.")
}

See Also

Supported Scripting Languages - Specifics of Usage

Highlight search results