VBScript Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.01

Array Declaration and Built-in Functions

Part:   1  2  3   4  5 

VB Script Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Data Types and Literals

Variables

Logic Operations

String Operations

Conditional Statements

Arrays

Loop Statements

Functions and Subroutines

Built-in Functions

Variable Inspection

... Table of Contents

(Continued from previous part...)

Dynamic-Size Array Example

To show you how dynamic-size array works, I wrote the following example, array_dynamic_size.html:

<html>
<body>
<!-- array_dynamic_size.html
   Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
   document.writeln("")
   Dim aYear()
   document.writeln("Check 1: Is aYear an array? " & IsArray(aYear))

'   aYear(0) = "Jan"   'Error 1: Subscription out of range

'   iSize = UBound(aYear)+1 'Error 2: Index out of range

   ReDim aYear(5)
   document.writeln("Check 2: Is aYear an array? " & IsArray(aYear))

   aYear(0) = "Jan"
   aYear(1) = "Feb"
   aYear(5) = "Jun"
   
   ReDim Preserve aYear(11)
   aYear(10) = "Nov"
   document.writeln("Months in a year:")
   For i=LBound(aYear) To UBound(aYear)
      document.writeln("   " & i & " = " & aYear(i))
   Next
</script>
</pre>
</body>
</html>

Here is the output:

Check 1: Is aYear an array? True
Check 2: Is aYear an array? True
Months in a year:
   0 = Jan
   1 = Feb
   2 = 
   3 = 
   4 = 
   5 = Jun
   6 = 
   7 = 
   8 = 
   9 = 
   10 = Nov
   11 = 

Note that:

  • Output message "Check 1" shows that a dynamic-size array is an array event if its size is not set yet.
  • Commented out "Error 2" shows that "UBound" can not be used is array's size is not set yet.

"Array" Function and "For Each" Example

To show you how the "Array" function and the "For Each" statement work, I wrote the following example, array_dynamic_size.html:

<html>
<body>
<!-- array_foreach.html
   Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
   document.writeln("")

   ' Creating a dynamic array with the "Array" function
   aSite = Array("yahoo", "netscape", "microsoft")
   document.writeln("Is aSite an array? " & IsArray(aSite))
   document.writeln("Lower bound of aPrime = " & LBound(aSite))
   document.writeln("Upper bound of aPrime = " & UBound(aSite))

   ' Resizing the array
   ReDim Preserve aSite(8)
   aSite(8) = "ibm"

   ' Updating array elements
   For Each sSite In aSite
      sSite = sSite & ".com"
   Next

   ' Retrieving array elements
   document.writeln("Web sites:")
   For Each sSite In aSite
      document.writeln("   " & sSite )
   Next
</script>
</pre>
</body>
</html>

(Continued on next part...)

Part:   1  2  3   4  5 

Dr. Herong Yang, updated in 2006
VBScript Tutorials - Herong's Tutorial Notes - Array Declaration and Built-in Functions