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:
If you think that the assignment statement is not valid, you are wrong.
Assigning an array to a scalar variable is valid.
If you think that scalar variable, aScalar, will become an array variable, you are wrong too.
Scalar variable, aScalar, will stay as a scalar variable. It will be used to store a reference
of an array.
If you think that scalar variable, aScalar, will hold a reference of the array represented
by array variable, anArray, you are wrong again.
A new dynamic-size array will be created as a copy of the array represented by array variable, anArray.
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) 2015, 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:
(TypeName(aScalar): String) shows that scalar variable, aScalar, holds a String value
before the assignment statement: (aScalar = anArray).
(TypeName(aScalar): Variant()) shows that scalar variable, aScalar, holds an array value
(indirectly through a reference) after the assignment statement: (aScalar = anArray).
(aScalar(0): Pig) and (anArray(0): Dog) prove that the array value referenced
by scalar variable, aScalar, is a copy of the original array.
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.