Chapter: Internet & World Wide Web HOW TO PROGRAM - VBScript

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Arrays - VBScript

Arrays are data structures consisting of related data items of the same type. A fixed-size ar-ray’s size does not change during program execution; a dynamic array’s size can change during execution.



Arrays are data structures consisting of related data items of the same type. A fixed-size ar-ray’s size does not change during program execution; a dynamic array’s size can change during execution. A dynamic array is also called a redimmable array (short for a “re-di-mensionable” array). Individual array elements are referred to by giving the array name fol-lowed by the element position number in parentheses, (). The first array element is at position zero.


The position number contained within parentheses is more formally called an index. An index must be in the range 0 to 2,147,483,648 (any floating-point number is rounded to the nearest whole number).


The declaration


Dim numbers( 2 )


instructs the interpreter to reserve three elements for array numbers. The value 2 defines the upper bound (i.e., the highest valid index) of numbers. The lower bound (the lowest valid index) of numbers is 0. When an upper bound is specified in the declaration, a fixed-size array is created.

The programmer can explicitly initialize the array with assignment statements. For example, the lines

numbers(0) = 77 numbers(1) = 68 numbers(2) = 55


initialize numbers. Repetition statements can also be used to initialize arrays. For exam-ple, the statements


Dim h( 11 ), x, i i = 0


For x = 0 To 30 Step 3 h(i) = CInt( x )


i = CInt( i ) + 1 Next


initializes the elements of h to the values 0, 3, 6, 9, …, 30.


The program in Fig. 24.18 declares, initializes and prints three arrays. Two of the arrays are fixed-size arrays and one of the arrays is a dynamic array. The program intro-duces function UBound, which returns the upper bound (i.e., the highest-numbered index). [Note: VBScript does provide function LBound for determining the lowest-numbered index. However, the current version of VBScript does not permit the lowest-numbered index to be non-zero.]


          <?xml version = "1.0"?>

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"



          <!--Fig. 24.18: arrays.html      -->

          <!--VBScript Arrays      -->


          <html xmlns = "">


          <title>Using VBScript Arrays</title>


          <script type = "text/vbscript">


          Option Explicit


          Public Sub DisplayArray( x, s )

          Dim j


          Document.Write( s & ": " )

          For j = 0 To UBound( x )

          Document.Write( x( j ) & " " )



          Document.Write( "<br />" )

          End Sub


          Dim fixedSize( 3 ), fixedArray, dynamic(), k


          ReDim dynamic( 3 )  ' Dynamically size array

          fixedArray = Array( "A", "B", "C" )


          ' Populate arrays with values

          For k = 0 to UBound( fixedSize )

          fixedSize( k ) = 50 - k

          dynamic( k ) = Chr( 75 + k )



          ' Display contents of arrays

          Call DisplayArray( fixedSize, "fixedSize" )

          Call DisplayArray( fixedArray, "fixedArray" )

          Call DisplayArray( dynamic, "dynamic" )


          ' Resize dynamic, preserve current values

          ReDim Preserve dynamic( 5 )

          dynamic( 3 ) = 3.343

          dynamic( 4 ) = 77.37443


          Call DisplayArray( dynamic, _

          "dynamic after ReDim Preserve" )





Fig. 24.18  Using VBScript arrays


Lines 16–25 define Sub procedure DisplayArray. VBScript procedures are Public by default; therefore, they are accessible to scripts on other Web pages. Keyword Public can be used explicitly to indicate that a procedure is public. A procedure can be marked as Private to indicate that the procedure can be called only from the XHTML document in which it is defined.


Procedure DisplayArray receives arguments x and s and declares local variable j. Parameter x receives an array and parameter s receives a string. The For header (line 20) calls function UBound to get the upper bound of x. The Document object’s Write method is used to print each element of x.

Line 27 declares a four element fixed-sized array named fixedSize (the value in parentheses indicates the highest index in the array, and the array has a starting index of 0), variants fixedArray and k, and dynamic array dynamic.


Statement ReDim (line 29) allocates memory for array dynamic (four elements, in this example). All dynamic array memory must be allocated via ReDim. Dynamic arrays are more flexible than fixed-sized arrays, because they can be resized anytime by using ReDim, to accommodate new data.


Line 30 creates an array containing three elements and assigns it to fixedArray. VBScript function Array takes any number of arguments and returns an array containing those arguments. Lines 39–41 pass the three arrays and three strings to DisplayArray. Line 44 reallocates dynamic’s memory to 5 elements. When keyword Preserve is used with ReDim, VBScript maintains the current values in the array; otherwise, all values in the array are lost when the ReDim operation occurs.

If ReDim Preserve creates a larger array, every element in the original array is pre-served. If ReDim Preserve creates a smaller array, every element up to (and including) the new upper bound is preserved (e.g., if there were 10 elements in the original array and the new array contains five elements, the first five elements of the original array are pre-served). Lines 45–46 assign values to the new elements. Procedure DisplayArray is called to display array dynamic.


Arrays can have multiple dimensions. VBScript supports at least 60 array dimensions, but most programmers will need to use only two- or three-dimensional arrays.

A multidimensional array is declared much like a one-dimensional array. For example, consider the following declarations

Dim b(2, 2), tripleArray(100, 8, 15)


which declares b as a two-dimensional array and tripleArray as a three-dimensional array. Functions UBound and LBound can also be used with multidimensional arrays. When calling UBound or LBound, the dimension is passed as the second argument. Array dimensions always begin at one. If a dimension is not provided, the default dimension 1 is used. For example, the For header


For x = 0 To UBound(tripleArray, 3)


would increment x from the third dimension’s lower bound, 0, to the third dimension’s up-per bound, 15.


Multidimensional arrays can also be created dynamically. Consider the declaration


Dim threeD()


which declares a dynamic array threeD. The number of dimensions is not set until the first time ReDim is used. Once the number of dimensions is set, the number of dimensions cannot be changed by ReDim (e.g., if the array is a two-dimensional array, it cannot be-come a three-dimensional array). The statement


ReDim threeD(11, 8, 1)


allocates memory for threeD and sets the number of dimensions at 3.

Memory allocated for dynamic arrays can be deallocated (released) at run-time using the keyword Erase. A dynamic array that has been deallocated must be redimensioned with ReDim before it can be used again. Erase can also be used with fixed-sized arrays to initialize all the array elements to the empty string. For example, the statement


Erase mDynamic


releases mDynamic’s memory.

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Copyright © 2018-2020; All Rights Reserved. Developed by Therithal info, Chennai.