ASP Tutorial Lesson 7: Collections

If you want to store a list in ASP there are two popular ways, arrays and dictionaries.

Arrays are numbered lists. You can think of them as numbered boxes to put things in and you can get at the information by using the index, for example myArray(3) would get whatever was in “slot” three of myArray.

One thing to keep in mind is that arrays start with 0 (zero) as the first index. To find out how many pieces of information is stored in your array you can use ubound(arrayname).

Dimension an array

dim arrayName(x)

Dimension an array using a variable

dim arrayName()
x=100
redim preserve arrayName(x)

Dimensioning and Loading an array with values

summerMonths=array(“June”,”July”,”August”)

See more info on variables in Tutorial 3.

Dictionaries

A more advanced list is the dictionary. This stores information not against a number key but against a name. You access the value by using its name key, eg. starSigns(“July”) would return “Leo”.

Create a dictionary

set myDictionary=server.createObject(“Scripting.Dictionary”)

Add an item

myDictionary.Add “key”,”value”

Access an items value

myDictionary.Item(“key”)

Find if an item exists

if myDictionary.Exists(“key”) then …

Remove an item

myDictionary.Remove(“key”)

Get an array of dictionary items

myArray=myDictionary.Items

Get an array of dictionary keys

myArray=myDictionary.Keys

 

For more information on arrays, check out

https://msdn.microsoft.com/en-us/library/aa478965.aspx