"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:

<!-- array_fixed_size.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
<script language="vbscript">
   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))

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

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


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:

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

Array Data Type and Related Statements

 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

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)


 PDF Printing Version