Array Size with UBound() and LBound()

UBound() returns the highest valid index in an array, while LBound() returns the lowest valid index. In Classic ASP (VBScript), array indexes start at 0. The upper bound is therefore not the same as the element count.

Quick rule: For an allocated one-dimensional array, UBound(items) - LBound(items) + 1 gives the number of available elements. A declaration such as Dim items(3) creates four positions: 0, 1, 2 and 3.

Quick example: find the array size

<%
Option Explicit
Dim countries
countries = Array("UK", "USA", "Canada", "UAE")
Response.Write "Highest index: " & UBound(countries) & "<br>"
Response.Write "Total elements: " & (UBound(countries) - LBound(countries) + 1)
%>

Output: Highest index: 3; Total elements: 4. The four values are stored at indexes 0 through 3.

Syntax of UBound() and LBound()

UBound(arrayName[, dimension])
LBound(arrayName[, dimension])

The optional dimension argument identifies which dimension to inspect: 1 is the first dimension, 2 is the second, and so on. If omitted, the first dimension is used.

ExpressionMeaning
UBound(arr)Highest valid index in the first dimension
LBound(arr)Lowest valid index in the first dimension (0 in VBScript)
UBound(arr) - LBound(arr) + 1Number of positions in a one-dimensional allocated array
UBound(arr, 2)Highest valid index in the second dimension

Highest index and loop limit

The following example preserves the original country-array demonstration. Dim myArray(3) allocates positions 0 through 3; it does not allocate three elements.

<%
Dim myArray(3), i
myArray(0) = "UK"
myArray(1) = "USA"
myArray(2) = "Canada"
myArray(3) = "UAE"

Response.Write "Highest index = " & UBound(myArray) & "<br>"
Response.Write "Number of elements = " & (UBound(myArray) - LBound(myArray) + 1)

For i = LBound(myArray) To UBound(myArray)
    Response.Write "<br>" & Server.HTMLEncode(myArray(i))
Next
%>

Output: Highest index = 3; Number of elements = 4; followed by UK, USA, Canada and UAE on separate lines. Using LBound() and UBound() avoids hard-coding loop limits.

Count an array created with Array() or Split()

Many Classic ASP programs create arrays from a list of values or by splitting a string. The counting formula works for both.

<%
Dim months, monthCount
months = Array("Jan", "Feb", "Mar")
monthCount = UBound(months) - LBound(months) + 1
Response.Write "Month count: " & monthCount
%>

Output: Month count: 3.

<%
Dim colours, colourCount
colours = Split("red,green,blue", ",")
colourCount = UBound(colours) - LBound(colours) + 1
Response.Write "Colours: " & colourCount
%>

Output: Colours: 3. See Split() in Classic ASP for delimiters and empty-string handling.

Find the size of a two-dimensional array

The original Dim arr(3, 4) example creates 4 rows and 5 columns, or 20 positions in total. Pass the dimension number to UBound() and LBound() separately.

<%
Dim arr(3, 4), rowCount, columnCount, totalElements
rowCount = UBound(arr, 1) - LBound(arr, 1) + 1
columnCount = UBound(arr, 2) - LBound(arr, 2) + 1
totalElements = rowCount * columnCount

Response.Write "Rows: " & rowCount & "<br>"
Response.Write "Columns: " & columnCount & "<br>"
Response.Write "Total elements: " & totalElements
%>

Output: Rows: 4; Columns: 5; Total elements: 20. These are available positions; some may still be unassigned (Empty).

Loop through a two-dimensional array

Use an index for each dimension. The example stores and displays a compact 2-by-3 grid.

<%
Dim grid(1, 2), rowIndex, colIndex
grid(0, 0) = "A1"
grid(0, 1) = "A2"
grid(0, 2) = "A3"
grid(1, 0) = "B1"
grid(1, 1) = "B2"
grid(1, 2) = "B3"

For rowIndex = LBound(grid, 1) To UBound(grid, 1)
    For colIndex = LBound(grid, 2) To UBound(grid, 2)
        Response.Write grid(rowIndex, colIndex) & " "
    Next
    Response.Write "<br>"
Next
%>

Reusable function to count elements

You can put the formula in a small function for a one-dimensional allocated array. This is the same approach used in the original tutorial.

<%
Function ArraySize(values)
    ArraySize = UBound(values) - LBound(values) + 1
End Function

Dim cities
cities = Array("Delhi", "Mumbai", "Hyderabad")
Response.Write "Number of cities: " & ArraySize(cities)
%>

Output: Number of cities: 3. This simple function is not intended for unallocated dynamic arrays; see the section on empty arrays below.

Populate an array, then display its size

This version fills all six elements of Dim arr(5) and displays their positions and values.

<%
Dim arr(5), i, arraySize
For i = LBound(arr) To UBound(arr)
    arr(i) = i * 10
Next

arraySize = UBound(arr) - LBound(arr) + 1
Response.Write "Size of array: " & arraySize & "<br>"
For i = LBound(arr) To UBound(arr)
    Response.Write "Element " & i & " = " & arr(i) & "<br>"
Next
%>

Output: Size of array: 6, followed by Element 0 = 0 through Element 5 = 50.

Resize a dynamic array with ReDim Preserve

Use Dim arr() when the final number of elements is not initially known. ReDim allocates the array; ReDim Preserve changes its size while retaining existing values.

<%
Dim arr()
ReDim arr(3)
arr(0) = "A"
arr(1) = "B"
arr(2) = "C"
arr(3) = "D"
Response.Write "Before resizing: " & (UBound(arr) - LBound(arr) + 1) & "<br>"

ReDim Preserve arr(5)
arr(4) = "E"
arr(5) = "F"
Response.Write "After resizing: " & (UBound(arr) - LBound(arr) + 1)
%>

Output: Before resizing: 4; After resizing: 6.

Important: When using ReDim Preserve on a multidimensional VBScript array, you can change only the last dimension. For instance, an allocated ReDim grid(1, 2) can be resized using ReDim Preserve grid(1, 4), not ReDim Preserve grid(2, 2).
Dim grid()
ReDim grid(1, 2)           ' 2 rows, 3 columns
ReDim Preserve grid(1, 4)  ' 2 rows, 5 columns; preserves values

Empty arrays versus unallocated dynamic arrays

Array() creates an empty array, for which UBound() is -1. By contrast, a dynamic array declared as Dim values() has not been allocated yet: calling UBound(values) before ReDim can cause a runtime error.

<%
Dim values
values = Array()
Response.Write "Empty array upper bound: " & UBound(values) & "<br>"

If UBound(values) < 0 Then
    Response.Write "The array has no elements."
End If
%>

Output: Empty array upper bound: -1; The array has no elements.

Dim values()
ReDim values(2)  ' Allocate 3 positions before calling UBound()
Response.Write UBound(values)  ' Displays 2

For general-purpose input where an array might be unallocated, validate or initialize it before applying the counting formula. IsArray() alone does not guarantee that a dynamic array has been allocated.

Common mistakes and troubleshooting

IssueWhat to check
Expecting Dim items(5) to contain 5 positionsIt creates 6 positions: indexes 0 through 5.
Subscript out of range when calling UBound()Check that a dynamic array has been allocated and you are using an existing dimension.
ReDim Preserve produces an error for 2D arraysOnly resize the last dimension when preserving contents.
Using the highest index as the element countUse UBound(arr) - LBound(arr) + 1, not just UBound(arr).
Count differs from the number of meaningful valuesThe formula counts available positions, including unassigned elements.

Frequently asked questions

Does UBound() return the number of elements?

No. It returns the highest index in the requested dimension. Dim x(3) has UBound(x) = 3 but has four positions.

Why do we use LBound() in the counting formula?

It makes the formula explicit: highest index minus lowest index plus one. In VBScript the lower bound of an array dimension is zero.

Can I use UBound() with the array returned by Split()?

Yes. For example, Split("a,b,c", ",") returns three elements. For an empty input, handle the empty-array result appropriately.

What happens if ReDim is used without Preserve?

The array is reallocated and previously stored values are not retained. Use ReDim Preserve if you need to keep them.

Can I use this formula on a multidimensional array?

Yes, calculate each dimension separately using the second dimension argument, and multiply those sizes for the total number of positions.

Continue with array declarations and related string operations:

Back to top ↑


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