Dimension and Array Essay

Submitted By yodizzle1234
Words: 3107
Pages: 13

Arrays in VBScript

Unlike all the other subtypes I showed you last month, an array can hold more than one value. An array lets you address many data values through the same variable. Think of an array as a list (e.g., a list of usernames). You reference each item in this list with a common name and index number. The common name is the name of the array variable. The common name typically includes the prefix arr to denote that the variable contains data of the subtype Array. The index number is an integer subscript that denotes an item's relative location in the list. The indexes always increment sequentially, starting from 0. For example, if an array has three items, the indexes are 0, 1, and 2. The 0 index represents the first position, the 1 index represents the second position, and the 2 index represents the third position in the array.
You can create two types of arrays: static and dynamic. Static arrays stay a fixed size throughout their lifetime—that is, the index size remains constant. Thus, when you create a static array, you must know how many items the array will contain throughout its lifetime. If you don't know this information or you know that the array's index size will change, you need to create a dynamic array. Dynamic arrays don't have a fixed index size. You can increase or decrease the index size at any time.
Creating Arrays
You can create arrays several ways, depending on whether they are static or dynamic. To create a static array, you use a Dim statement with the array's common name, such as
Dim arrGroc(2)
The number in parentheses specifies the maximum value of indexes in the array. The value of 2 denotes that the array's indexes range from 0 to 2, which means the array can contain up to three items. In other words, 2 is the array's upper bound. Arrays also have a lower bound. The lower bound of static (and dynamic) VBScript arrays is always 0 because arrays always start with the 0 index.
When you create a static array, the lower and the upper bound are 0 by default and the array is empty. Once you set a static array's upper bound, you can't change it because the static array's index size remains constant.
You can create a dynamic array in three ways:
1. Use Dim without an upper bound.
One way to create a dynamic array is to use a Dim statement in which you don't specify the array's upper bound. For example, to create the dynamic array arrUsers, you can use the statement
Dim arrUsers()
However, this array is uninitialized, which means you won't be able to use it until you set its size. To set the array's size, you use a ReDim statement to set the upper bound. For example, if you want to set an upper bound of 2 for arrUsers, you specify
ReDim arrUsers(2)
Although you set arrUsers' index size, the array is empty.

2. Use ReDim alone.
You can use ReDim without Dim to create a dynamic array that has an initial index size. For example, the code
ReDim arrUsers(2) creates a dynamic array whose initial size is 3. The arrUsers array is empty.
You typically use ReDim when you already know a useful initial size for the dynamic array. Using ReDim is more direct and saves you from adding an extra line of code.
3. Use Array.
You can use the Array function with Dim to simultaneously create and fill dynamic arrays. Dim creates and names the array, and Array fills its indexes. For example, to create arrUsers and assign the names of Mary, Bob, and Ann to its indexes, you use the code
Dim arrUsers arrUsers = Array("Mary", "Bob", "Ann")
Note that the code doesn't explicitly mention the size of the array. VBScript determines the array's size by the number of arguments (e.g., "Mary" is an argument) that you pass to the Array function. Keep in mind that the returned array contains only those items that you pass as arguments.
Table 1 on the Win32 Scripting Journal Web site summarizes the options for creating dynamic and static arrays. Because the table mentions details such as whether you need to…