∟"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:
A new dynamic-size array is created with those elements specified in the argument list.
The reference of the new array is stored to the scalar variable.
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.