RSS Feed

VBScript tip: Working with an object list

November 26, 2014 | Bretislav Valek

In some Reliance applications, it is needed that data in a script should be stored in a list with a variable number of elements and then gone through, searched for, or deleted. To meet this need, you can create an array-type tag and program functions for the respective operations. However, this is laborious and especially useless because there are ready-to-use solutions in the form of Scripting.Dictionary objects or several .NET Framework classes, such as System.Collections.ArrayList.


Scripting.Dictionary is a list object that allows storing and then searching for paired data (key-value). The Scripting.Dictionary object is part of the VBScript scripting engine with no need for installation. The following example shows how to use the object when working with a list of users (user name = key, email address = value).


Dim recipients, email, items, i, s

' Creates an object.
Set recipients = CreateObject("Scripting.Dictionary")

' Fills the list.
recipients.Add "bill", "bill.gates@microsoft.com"
recipients.Add "mark", "mark.zuckerberg@facebook.com"
recipients.Add "larry", "larry.page@google.com"
recipients.Add "steve", "steve.jobs@apple.com"

' Searches for the user larry.
email = recipients.Item("larry")

' Displays the search result.
MsgBox "You can email larry on " & email, vbSystemModal

Dictionary1

' Changes the address of the user bill.
recipients.Key("bill") = "bill@gatesfoundation.org"

' Removes the user mark.
recipients.Remove "mark"

' Carries out sorting.
SortDict recipients

' Goes through the list and finds out all addresses.
s = ""
items = recipients.Items
For i = 0 To recipients.Count - 1
  s = s & " " & items(i)
Next
MsgBox "All recipents are: " & s, vbSystemModal

Dictionary2

' Releases the list.
Set recipients = Nothing

The Scripting.Dictionary object does not support sorting the list elements. If you need to sort the list elements, the SortDict function can be implemented:


' A function for sorting elements within the Scripting.Dictionary object.
Function SortDict(ByVal dict)
  Dim i, j, temp
    For Each i In dict
      For Each j In dict
        If(dict.Item(i) <= dict.Item(j)) Then
          temp = dict.Item(i)
          dict.Item(i) = dict.Item(j)
          dict.Item(j) = temp
        End If
      Next
    Next
    Set SortDict = dict
End Function

The sorting itself is carried out by simply calling the SortDict function:


SortDict recipients

The above example shows how to work with an object list of type string. Of course, objects of another type can be added to the list. They are, for example, numbers and predefined or user-defined objects (classes). But do not forget to adjust the program code accordingly.


The .NET Framework runtime environment contains other objects for working with the list. They can be used in VBScript, which means they can also be used in Reliance:


  • System.Collections.ArrayList
  • System.Collections.SortedList
  • System.Collections.Stack
  • System.Collections.Queue

All these objects require that .NET Framework (version 1.0 or higher) be installed.



System.Collections.ArrayList is a list of type array. Unlike the Scripting.Dictionary object, this one does not work with paired data (key-value).


Dim Message, ArrayList

' Initializes the message.
Message = ""

' Creates a list.
Set ArrayList = CreateObject("System.Collections.ArrayList")

' Fills the list.
ArrayList.Add 6
ArrayList.Add 8
ArrayList.Add 2
ArrayList.Add 4
ArrayList.Add 1
ArrayList.Add 5
ArrayList.Add 3
ArrayList.Add 3
ArrayList.Add 7

' Converts the list into an array.
Message = Message & "1: " & Join(ArrayList.ToArray, ",") & vbCrLf

' Returns the element at the specified index.
Message = Message & "2: " & ArrayList(0) & vbCrLf

' Returns the element count.
Message = Message & "3: " & ArrayList.Count & vbCrLf

' Finds out whether an element is contained in the list.
Message = Message & "4: " & ArrayList.Contains(5) & vbCrLf

' Sorts the list.
ArrayList.Sort
Message = Message & "5: " & Join(ArrayList.ToArray, ",") & vbCrLf

' Reverses the sorting direction.
ArrayList.Reverse
Message = Message & "6: " & Join(ArrayList.ToArray, ",") & vbCrLf

' Removes the element.
ArrayList.Remove 8

' Removes the element at the specified index.
ArrayList.RemoveAt 6

' Removes all elements.
ArrayList.Clear

' Releases the list.
Set ArrayList = Nothing

' Displays the result.
MsgBox Message, vbSystemModal

ArrayList


System.Collections.SortedList allows working with paired data and, at the same time, accessing the list items using an index. In terms of capabilities, it is a combination of Scripting.Dictionary and and System.Collections.ArrayList. Each time you add a pair to the list, the list is sorted automatically.


Dim Message, SortedList, i, s

' Initializes the message.
Message = ""

' Creates a list.
Set SortedList = CreateObject("System.Collections.SortedList")

' Fills the list.
SortedList.Add "Point", 1
SortedList.Add "Point Cloud", 2
SortedList.Add "Curve", 4
SortedList.Add "Surface", 8
SortedList.Add "Polysurface", 16
SortedList.Add "Mesh", 32

' Returns the element count.
Message = Message & "1: " & SortedList.Count & vbCrLf

' Returns the value by key.
Message = Message & "2: " & SortedList("Surface") & vbCrLf

' Returns the value at the index.
s = ""
For i = 0 To SortedList.Count - 1
  s = s & CStr(SortedList.GetByIndex(i)) & " "
Next
Message = Message & "3: " & s & vbCrLf

' Verifies the existence of the key.
Message = Message & "4: " & SortedList.ContainsKey("Polysurface") & vbCrLf

' Verifies the existence of the value.
Message = Message & "5: " & SortedList.ContainsValue(16) & vbCrLf

' Returns the index of the key (searches for the element by key).
Message = Message & "6: " & SortedList.IndexOfKey("Polysurface") & vbCrLf

' Returns the index of the value (searches for the element by value).
Message = Message & "7: " & SortedList.IndexOfValue(16) & vbCrLf

' Removes the element by key.
SortedList.Remove "Polysurface" & vbCrLf

' Removes the element at the specified index.
SortedList.RemoveAt 0

' Removes all elements.
SortedList.Clear

' Releases the list.
Set SortedList = Nothing

' Displays the result.
MsgBox Message, vbSystemModal

SortedList


System.Collections.Stack is an object list of type stack, i.e., last-in-first-out (LIFO). The last elements inserted into the stack will be the first to be removed.


Dim Message, Stack, Item, s

' Initializes the message.
Message = ""

' Creates a list.
Set Stack = CreateObject("System.Collections.Stack")

' Fills the list.
Stack.Push "Item_1"
Stack.Push "Item_2"
Stack.Push "Item_3"
Stack.Push "Item_4"

' Goes through the list elements.
s = ""
For Each Item In Stack
  s = s & Item & " "
Next
Message = Message & "1: " & s & vbCrLf

' Converts the list into an array.
Message = Message & "2: " & Join(Stack.ToArray, ",") & vbCrLf

' Returns the element count.
Message = Message & "3: " & Stack.Count & vbCrLf

' Verifies the existence of the value.
Message = Message & "4: " & Stack.Contains("Item_2") & vbCrLf

' Removes and returns the last element.
Message = Message & "5: " & Stack.Pop & vbCrLf

' Returns the last element (without removing it).
Message = Message & "6: " & Stack.Peek & vbCrLf

' Empties the list.
Stack.Clear

' Releases the list.
Set Stack = Nothing

' Displays the result.
MsgBox Message, vbSystemModal

Stack


System.Collections.Queue is an object list of type queue, i.e., first-in-first-out (FIFO). The first elements inserted into the queue will be the first to be removed.


Dim Message, Queue, Item, s

' Initializes the message.
Message = ""

' Creates a list.
Set Queue = CreateObject("System.Collections.Queue")

' Fills the list.
Queue.Enqueue "Item_1"
Queue.Enqueue "Item_2"
Queue.Enqueue "Item_3"
Queue.Enqueue "Item_4"


' Goes through the list elements.
s = ""
For Each Item In Queue
  s = s & Item & " "
Next
Message = Message & "1: " & s & vbCrLf


' Converts the list into an array.
Message = Message & "2: " & Join(Queue.ToArray, ",") & vbCrLf

' Returns the element count.
Message = Message & "3: " & Queue.Count & vbCrLf

' Verifies the existence of the value.
Message = Message & "4: " & Queue.Contains("Item_2") & vbCrLf

' Removes and returns the first element.
Message = Message & "5: " & Queue.Dequeue & vbCrLf

' Returns the first element (without removing it).
Message = Message & "6: " & Queue.Peek & vbCrLf

' Empties the list.
Queue.Clear

' Releases the list.
Set Queue = Nothing

' Displays the result.
MsgBox Message, vbSystemModal

Queue
Reliance Earth Icon

Top