VBScript Tutorials - Herong's Tutorial Examples - v6.03, by Herong Yang
Assigning an Array to a Scalar Variable
This section provides a tutorial example on how to assign an array to a scalar variable. Actually, a copy of the original array is created and the reference of the copy if assigned the scalar variable.
What will happen, if you try to assign an array to a scalar variable with an assignment statement?
Read the following VBScript code example and guess what will happen:
Dim aScalar, anArray(1) aScalar = anArray
VBScript has several surprise for us:
To show you how an array can be assigned to a scalar variable, I wrote the following example, array_assignment.html:
<html>
<body>
<!-- array_assignment.html
- Copyright (c) 1998 HerongYang.com. All Rights Reserved.
-->
<pre>
<script language="vbscript">
Dim aScalar
Dim anArray(1)
aScalar = "Rabbit"
anArray(0) = "Dog"
anArray(1) = "Cat"
document.writeln()
document.writeln("Before the assignment and changes:")
document.writeln(" TypeName(aScalar): " & TypeName(aScalar))
document.writeln(" TypeName(anArray): " & TypeName(anArray))
aScalar = anArray 'A new dynamic-size array is created
'The reference of the new array is in aScalar
aScalar(0) = "Pig" 'Array reference works like an array
ReDim Preserve aScalar(2) 'The referenced array is re-sized
aScalar(2) = "Fox"
document.writeln()
document.writeln("After the assignment and changes:")
document.writeln(" TypeName(aScalar): " & TypeName(aScalar))
document.writeln(" TypeName(anArray): " & TypeName(anArray))
document.writeln(" aScalar(0): " & aScalar(0))
document.writeln(" aScalar(1): " & aScalar(1))
document.writeln(" aScalar(2): " & aScalar(2))
document.writeln(" anArray(0): " & anArray(0))
document.writeln(" anArray(1): " & anArray(1))
</script>
</pre>
</body>
</html>
Here is the output:
Before the assignment and changes: TypeName(aScalar): String TypeName(anArray): Variant() After the assignment and changes: TypeName(aScalar): Variant() TypeName(anArray): Variant() aScalar(0): Pig aScalar(1): Cat aScalar(2): Fox anArray(0): Dog anArray(1): Cat
The output shows that:
Note that in some other languages like Java, assigning an array to a new variable creates a new reference to the original array. No new array will be created.
Table of Contents
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
"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
IE Web Browser Supporting VBScript