Array References Work Like Arrays
This section provides a tutorial example on how to use an array reference like an array to access referenced array elements, to re-size the referenced array, and to pass the referenced array into functions.
After learning that a scalar variable can be used to store the reference of an array,
let's try to learn more about array reference:
- An array reference is a scalar value. This is why it can be assigned to a scalar variable.
- A scalar variable holding an array reference can be used like an array variable.
- "array_reference(i)" represents the element of index i of the referenced array.
- "ReDim Preserve array_reference(n)" resets the size of the referenced array.
- When an array reference is used as a function argument, the referenced array is passed
into the function, not the reference. For example, "TypeName(array_reference)" returns
To show you how an array reference works
I wrote the following example, array_reference.html:
- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
anArray(0) = "Dog"
anArray(1) = "Cat"
aReference = anArray 'An array reference is assigned
aReference(0) = "Pig" 'Accessing an element through the reference
ReDim Preserve aReference(2) 'Re-sizing the referenced array
aReference(2) = "Fox"
upperLimit = UBound(aReference) 'The referenced array is passed
document.writeln("TypeName(aReference): " & TypeName(aReference))
document.writeln("UBound(aReference): " & upperLimit)
For Each e In aReference
document.writeln(" " & e )
Here is the output:
The output confirms that all notes mentioned earlier in this section are true.
Table of Contents
About This Book
Introduction of VBScript - Visual Basic Scripting Edition
Variant Data Type, Subtypes, and Literals
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
►Array References and Array Assignment Statements
Assigning an Array to a Scalar Variable
►Array References Work Like Arrays
"Array()" Function - Returning a Scalar Reference of an Array
Creating a Copy of an Array
"Type mismatch" Runtime Error - Assignments to Array Variables
Conditional Statements - "If ... Then" and "Select Case"
Loop Statements - "For", "While", and "Do"
"Function" and "Sub" Procedures
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