VBScript Arrays in Classic ASP

An array stores several related values under one variable name. In VBScript, array indexes are zero-based by default, so Dim myArray(1) creates two elements: myArray(0) and myArray(1).

Fixed-size array

Dim myArray(1)

Assign values by index:

myArray(0) = "UK"
myArray(1) = "USA"

Writing beyond the declared upper bound raises a subscript error. If the number of elements must change while the script runs, declare a dynamic array and size it with ReDim.

Dynamic array with ReDim

Dim myArray() 'Declare a dynamic array
ReDim myArray(1) 'Allocate indexes 0 and 1
myArray(0) = "UK"
myArray(1) = "USA"

A fixed-size array cannot be resized with ReDim:

Dim myArray(1) 'Fixed-size array
ReDim myArray(2) 'Raises an error

Resize while preserving values

Dim myArray() 'Dynamic array
ReDim myArray(1)
myArray(0) = "UK"
myArray(1) = "USA"

ReDim Preserve myArray(3)
myArray(2) = "Canada"
myArray(3) = "UAE"

For Each item In myArray
    Response.Write item & "<br>"
Next

ReDim Preserve keeps existing values while changing the upper bound. For multi-dimensional arrays, preserving values has additional restrictions, so plan the array dimensions before resizing.

Array tutorials

TopicUse
UBound() and LBound()Find bounds and calculate the number of elements.
Split()Create an array by breaking a string.
Join()Combine array elements into one string.
Filter()Return array elements that include or exclude matching text.

ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer