ASP Vbscript ReDim Array

Updated on 20 Feb 2012,
Published on 25 Jul 2008

ASP Vbscript Redim statement for arrays is used to redefine the array size. You can increase the size of the vbscript dynamic array in asp using redim statement. Vbscript redim statement works only with dynamic arrays declared with Dim statement with any array size defined. There is no limit for vbscript array to redefine their size. You can increase the size of the array using ReDim statement dynamically within the vbscript code and you can assign value at new memory locations created within the array index declared in the variable declaration statement.

Classic ASP VbScript Array Functions Examples:

You can see the live samples and examples of Classic ASP VbScript Array Functions from the following links:

Syntax for ASP Vbscript Redim Array

Dim myArray()
ReDim myArray(10)

Redim statement will increase the size of the dynamic array to 10, having no size defined in the first statement. After redim declaration, myArray can store the 11 array items.

You can store the variant type data items in the Vbscript arrays. See the following example to learn the Redim statement for array and variant type values store in it.

Example of ASP Vbscript Redim Array

<%
Dim myArray()

Redim myArray(2)

myArray(0) = "array item 1"

myArray(1) = 1

myArray(2) = "array item 3"
 
ReDim myArray(5)

myArray(3) = 3

myArray(4) = "array item 5"

myArray(5) = "array item 6"

Response.Write "myArray(2): " & myArray(2) & "<br />"

Response.Write "myArray(5): " & myArray(5) & "<br />"

%>

In the above example myArray is declared as dynamic array in the vbscript code for asp web page. In the second statement Redim is used to set the array size =2 that can store 2 items. Then 3 values are assigned to 0, 1 and 2 index place of the myArray. Redim statement is used again to increase the size of myArray to 5 so that it could store 6 items. Then again 3 more values are assigned to the 3rd, 4th and 5th number index of myArray.

It will not display the value at array index 2 because when size of dynamic array is changed by used ReDim statement it looses the data stored in the array.

Output:

You can see the output of above discussed code from the following link:

VbScript Redim Array

Continue to next tutorial: ASP Vbscript ReDim Preserve Array to learn how to increase the length of dynamic array without loosing old values.

2 Responses to "ASP Vbscript ReDim Array"
Adrian
Why does everyone who "explains" the ReDim statement do so in such a confusing way? Here we are told "Redim statement will increase the size of the dynamic array to 10, .... After redim declaration, myArray can store the 11 array items." If the size is 10, how come it can hold 11? That is like telling me "the size of this glass is one pint, it can hold one and a half pints".
Ezineasp.net
Hi Adrian Here Redim myArray(2) means array of size 2 + 1 coz index of arrays start from 0 upto the specified size. So, myArray(2) has 3 spaces to store the items starting from 0th index, then 1st and last 2nd.
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email