Multidimensional Arrays in Classic ASP

Last updated: August 25, 2026.

A multidimensional VBScript array stores values in rows and columns. In Classic ASP, this is useful for small in-memory tables, query results, and grouped values that need more than one index.

Declare and populate a two-dimensional array

Dim cars(2, 2) creates two dimensions whose indexes both run from 0 through 2. In this example, the first dimension identifies a field and the second identifies a row.

Dim cars(2, 2)

' First dimension: field (name, year, price)
' Second dimension: row
cars(0, 0) = "BMW"
cars(1, 0) = 2024
cars(2, 0) = 45000

cars(0, 1) = "Audi"
cars(1, 1) = 2022
cars(2, 1) = 32000

cars(0, 2) = "Mini"
cars(1, 2) = 2025
cars(2, 2) = 28000

Choose one dimension order and use it consistently. A short comment such as cars(field, row) prevents indexing mistakes.

Loop through one dimension

Pass the dimension number to LBound() and UBound(). Here, dimension 2 contains the rows:

Dim row
Response.Write "<table>"
Response.Write "<thead><tr><th>Car</th><th>Year</th><th>Price</th></tr></thead>"
Response.Write "<tbody>"

For row = LBound(cars, 2) To UBound(cars, 2)
    Response.Write "<tr>"
    Response.Write "<td>" & Server.HTMLEncode(CStr(cars(0, row))) & "</td>"
    Response.Write "<td>" & cars(1, row) & "</td>"
    Response.Write "<td>" & FormatCurrency(cars(2, row)) & "</td>"
    Response.Write "</tr>"
Next

Response.Write "</tbody></table>"

Values written into HTML should be encoded when they can contain text from a user or database. Numeric values can be formatted after validation.

Resize a multidimensional array

Dim grid()
ReDim grid(2, 0)

grid(0, 0) = "BMW"
grid(1, 0) = 2024
grid(2, 0) = 45000

' Preserve works because only the last dimension changes.
ReDim Preserve grid(2, 1)
grid(0, 1) = "Audi"
grid(1, 1) = 2022
grid(2, 1) = 32000

With Preserve, VBScript can change only the upper bound of the final dimension. If both dimensions must grow freely, consider an array of arrays, a Dictionary, or processing the database recordset directly.

Bounds and dimensions

  • LBound(cars, 1) and UBound(cars, 1) inspect the first dimension.
  • LBound(cars, 2) and UBound(cars, 2) inspect the second dimension.
  • The declared number is an upper bound, so an upper bound of 2 provides three positions.

Start with one-dimensional Classic ASP arrays if you need examples of Array(), ReDim, and ReDim Preserve.

admin

admin

Leave a Reply

Your email address will not be published.