How to Use a Variable as a Dictionary Key in VBScript

VBScript is a scripting language that borrows some of the syntax from Visual Basic. A Visual Basic dictionary is a data container that stores data as key-value pairs. This is similar to a real dictionary in which the word you are looking up is the key and the definition is the value. When you interface with dictionaries, it is often easiest to use a variable to store the key for the value. You can then use the variable to access any value held by the dictionary.

Instructions

    • 1

      Open a new text file using a text editor. Save the file as “example.vbs.”

    • 2

      Write the following opening and closing VBScript tags. The VBScript must go in between these tags.

      <%
      %>

    • 3

      Write a statement that defines a variable named “animals.” This variable will be used to store the dictionary.

      Dim animals

    • 4

      Write the following statement to turn “animals” into a dictionary:

      Set animals = CreateObject("Scripting.Dictionary")

    • 5

      Write some statements that add key-value pairs to the dictionary, such as this:

      animals.Add "a", "Cat"
      animals.Add "b", "Dog"
      animals.Add "c", "Elephant"

    • 6

      Write a variable declaration that stores a key to the dictionary, such as this:

      Dim animalsKey = “c”

    • 7

      Print out the value associated with the key using the following statement:

      Response.Write animals.Item(animalsKey)

Related Searches:

References

Comments

Related Ads

Featured