"Array()" Function - Returning a Scalar Reference of an Array

This section provides a tutorial example on how to use 'Array()' function to return a scalar reference of new dynamic-size array. The returned array reference can be used like an array.

After learning how an array reference works, we are ready to look that the "Array(...)" function as shown in the following statement:

   scalar_name = Array(element_1, element_2, ...)

The above statement does the following:

To show you how the Array() function works, I wrote the following example, array_reference.html:

<html>
<body>
<!-- array_function.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
   Dim aReference
   
   ' Creates a new dynamic-size array 
   ' Returns the reference of the new array
   aReference = Array("yahoo", "netscape", "microsoft")

   document.writeln("TypeName(aReference): " & TypeName(aReference))
   document.writeln("UBound(aReference): " & UBound(aReference))
   For Each e In aReference
      document.writeln("   " & e )
   Next
   
   document.writeln("Re-sizing the referenced array...")
   ReDim Preserve aReference(3)
   aReference(3) = "google.com"
   
   document.writeln("TypeName(aReference): " & TypeName(aReference))
   document.writeln("UBound(aReference): " & UBound(aReference))
   For Each e In aReference
      document.writeln("   " & e )
   Next
</script>
</pre>
</body>
</html>

Here is the output:

TypeName(aReference): Variant()
UBound(aReference): 2
   yahoo
   netscape
   microsoft
Re-sizing the referenced array...
TypeName(aReference): Variant()
UBound(aReference): 3
   yahoo
   netscape
   microsoft
   google.com

The output confirms that the "Array()" function returns a scalar reference to a new dynamics-size array. This array reference variable can be used like an array variable.

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

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

 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)

 References

 PDF Printing Version