Array Declaration and Built-in Functions
Part:
1
2
3
4
5
(Continued from previous part...)
Here is the output:
Is aSite an array? True
Lower bound of aPrime = 0
Upper bound of aPrime = 2
Web sites:
yahoo
netscape
microsoft
ibm
Noticed anything interesting? The example confirms that:
- Arrays created with the "Array" function are dynamic-size arrays.
- "For Each" statement creates a copy of the current element in the temporary variable.
You can not update element values in this way.
My "Updating array elements" code block updated only the temporary variable.
"Erase" Statements
It is interesting to know that VB offers a special statement called "Erase" to remove all values
in an array:
Erase array_variable
If the specified array is a fixed-size array, it will reset all elements to the default value.
If the specified array is a dynamic-size array, it will reset the array to zero size.
To show you how the "Erase" statements work, I wrote the following example, array_erase.html:
<html>
<body>
<!-- array_erase.html
Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
' Setting a fixed-size array
Dim aFixed(1)
aFixed(0) = "Dog"
aFixed(1) = "Cat"
document.writeln("")
document.writeln("Favorite pets:")
For Each sItem In aFixed
document.writeln(" " & sItem )
Next
' Setting a dynamic-size array
aDynamic = Array("Apple", "Orange")
document.writeln("")
document.writeln("Favorite fruits:")
For Each sItem In aDynamic
document.writeln(" " & sItem )
Next
' Erasing arrays
document.writeln("")
document.writeln("Erasing arrays...")
Erase aFixed
Erase aDynamic
' Looking at arrays again
document.writeln("")
document.writeln("Pets left:")
iCount = 0
For Each sItem In aFixed
iCount = iCount + 1
document.writeln(" " & sItem )
Next
document.writeln(" Count = " & iCount)
document.writeln("")
document.writeln("Fruits left:")
iCount = 0
For Each sItem In aDynamic
iCount = iCount + 1
document.writeln(" " & sItem )
Next
document.writeln(" Count = " & iCount)
</script>
</pre>
</body>
</html>
(Continued on next part...)
Part:
1
2
3
4
5
|