There is an alternative to tracking the size of
the array using a variable (in this case, intIndex)
The UBound function returns the highest index number of an array. Bearing in
mind that the first element of the array is 0 we can determine the size of an
array with:
myArraysize=UBound(array)+1
Click here to see the application rewritten to use this function (this is a client-side script and the source can be viewed in the browser)
<HTML>
<HEAD>
<TITLE>Dynamic
Array Application</TITLE>
<SCRIPT LANGUAGE = "vbscript">
OPTION EXPLICIT
'require all variables to be declared
ReDim myArray(0) 'create a dynamic array with 1 element
Dim intIndex 'variable to track the array index
intIndex = 0 'assign the first index number to counter
Sub first_button_OnClick
' Store the user input in the array
myArray(intIndex)
= Document.fenform.fentext.Value
intIndex =
intIndex + 1 'increment the array counter by one
Dim Preserve
myArray(intIndex) 'increase the size of the array
Document.fenform.fentext.Value
= "" 'Empty the text box again
End Sub
Sub second_button_OnClick
Dim x, y, strArrayContents 'declare some variables we'll need
'repeat this
process as many times as there are array elements
'note: the last
element will always be empty because we've
'incremented
the counter *after* the assignment.
'try changing
the above sub so that we always fill every element
For x = 0 to
intIndex - 1
'
Assign a short description and the element no to the variable
StrArrayContents
= StrArrayContents & "Element No." & CStr(x) & " =
"
'add to this the contents of the
element
StrArrayContents = StrArrayContents
& myArray(x) & vbCRLF
'go
back and do it again for the next value of x
Next
'when we're
done show the result in a message box
y =
MsgBox(strArrayContents,0,"Dynamic Array Application")
End Sub
</SCRIPT>
</HEAD>
<BODY BGCOLOR="white">
<FORM NAME="fenform">
<INPUT TYPE="text" NAME="fentext"
><BR>
<INPUT TYPE="button" NAME="first_button"
VALUE="Add to array"><P>
<INPUT TYPE="button" NAME="second_button"
VALUE="Show Array Contents">
</FORM>
</BODY>
</HTML>