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

"Dim x(n)" - Fixed-Size Array Example

This section provides a tutorial example on how to declare a fixed-size array with 'Dim x(n)'. 'ReDim x(n)' statement can not be used on fixed-size arrays to change their sizes.

We learned from previous sections that a fixed-size array must be declared with a size. For example, "Dim x(99)" declares a fixed-size array, x, with 99 as the upper bound limit. The size of x is 100, because the lower bound is 0.

One important nature of a fixed-size array is that it can not be re-sized with a "ReDim x(n)" statement.

To show you how a fixed-size array works, I wrote the following example, array_fixed_size.html:

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

   aWeek(0) = "Sun"
   aWeek(4) = "Thu"
   aWeek(6) = "Sat"
   aWeek(1) = "Mon"

   document.writeln("Days in a week:")
   For i=LBound(aWeek) To UBound(aWeek)
      document.writeln("   " & i & " = " & aWeek(i))
   Next

'   aWeek(10) = "Abc"   'Error 1: Subscription out of range

'   ReDim Preserve aWeek(10) 'Error 2: This array is fixed

</script>
</pre>
</body>
</html>

Here is the output:

Is aWeek an array? True
Days in a week:
   0 = Sun
   1 = Mon
   2 = 
   3 = 
   4 = Thu
   5 = 
   6 = Sat

Note that there are two errors commented out in the VBScript example:

  • Error 1 - Array index (subscription) must be in the range of lower bound and upper bound.
  • Error 2 - Fixed-size array can not be resized with "ReDim" statements.

Sections in This Chapter

What Is an Array?

"Dim x()" - Declaring Array Variables

"x(i)" - Accessing Array Elements with Indexes

"Dim x(n)" - Fixed-Size Array Example

"Dim x()" - Dynamic-Size Array Example

"For Each" Statement Example

"Erase" Statement - Removing All Elements in an Array

Data Type "Variant()" - Array of Variant Values

Dr. Herong Yang, updated in 2008
"Dim x(n)" - Fixed-Size Array Example