David,
in addition to your other good answers, there's the uBound() function, which
will allow for you dimensioning an array differently at different times by
always returning the upper bound of the array index.
also, if you have varying numbers of elements to store, instead of
redimensioning arrays to handle this, you could look into scripting
dictionaries. they are an object, you use
createObject("scripting.dictionary"), which act something like arrays. they
have many advantages over arrays, including they can hold any number of
elements, and you don't have to dimension them ahead of time.
they are slower to access, so some functions which access arrays many
thousands of times would run noticeably slower with a dictionary. they do
however have a .count property, which will tell you how many elements they
currently have.
hth,
Chip
_____
From: David [mailto:[email protected]]
Sent: Tuesday, December 22, 2009 4:07 AM
To: Scripting List WE
Subject: VBScript - how many elements in an array
Hi, listers!
In VBS, when having an array, that might hold different amounts of elements:
Is there an easy way to tell how many elements the array holds, at any time.
With strings, for instance, we have the Len function, so as to tell how many
characters the string holds. Just wondering, if there is something similar
for arrays?
The other way around, is of course, the following example code; but I find
that is to envent the wheel every time.
---sample starts---
dim XArray: Array( 1,2,3,4,5 )
dim I, Number
Number = 0
For Each I In XArray
Number = Number + 1
Next
MsgBox "Array holds " & Number & " elements."
---sample ends---