VBScript: Multidimensional Arrays, Statements, and Commenting - Dynamic Arrays
(Page 2 of 6 )
Dynamic arrays are arrays whose size can be altered. To do so, you first initialize the array with no elements, then use ReDim to change its size, like so:
<html>
<body>
<script type="text/vbscript">
Dim cow()
Redim cow(2)
cow(0)="Moo"
cow(1)="Moo Moo"
cow(2)="Moo Moo Moo"
Redim Preserve cow(3)
cow(3)="Moo Moo Moo Moo"
</script>
</body>
</html>
The above code initially creates a dynamic array named cow, with no elements. We then use ReDim to change the number of elements within cow to 3 (cow(2)). Then we add some values to the array. Finally, we decide to add even more elements to cow, and ReDim it again to hold 4 values. We add the fourth value to the array, making sure to use the Preserve keyword. Had we not used Preserve, then all of the data in our array would have been deleted when we Redimmed it.
Next: Conditional Statements >>
More BrainDump Articles
More By James Payne