UBound
 
Returns the upper bound of an array's dimension

Syntax

Declare Function UBound ( array() As Any, ByVal dimension As Integer = 1 ) As Integer

Usage

result = UBound( array [, dimension ] )

Parameters

array
an array of any type
dimension
the dimension to get upper bound of

Return Value

Returns the upper bound of an array's dimension.

Description

UBound returns the largest value that can be used as an index into a particular dimension of an array.

Array dimensions are numbered from one (1) to n, where n is the total number of dimensions. If dimension is not specified, UBound will return the upper bound of the first dimension. If dimension is less than one (1) or greater than the total number of dimensions (n) in the array, the result is undefined.

Example


Dim array(-10 To 10, 5 To 15, 1 To 2) As Integer

Print UBound(array) 'returns 10
Print UBound(array, 2) 'returns 15
Print UBound(array, 3) 'returns 2


'' determining the size of an array
Dim As Short array(0 To 9)
Dim As Integer arraylen, arraysize

arraylen = UBound(array) - LBound(array) + 1
arraysize = arraylen * SizeOf( Short )

Print "Number of elements in array:", arraylen    '10
Print "Number of bytes used in array:", arraysize '10 * 2 = 20 


'' determining the size of a multi-dimensional array
Dim As Long array4D(1 To 2, 1 To 3, 1 To 4, 1 To 5)
Dim As Integer arraylen, arraysize


arraylen = (UBound(array4D, 4) - LBound(array4D, 4) + 1) _
         * (UBound(array4D, 3) - LBound(array4D, 3) + 1) _
         * (UBound(array4D, 2) - LBound(array4D, 2) + 1) _
         * (UBound(array4D, 1) - LBound(array4D, 1) + 1)

arraysize = arraylen * SizeOf( Long )

Print "Number of elements in array:", arraylen    '2 * 3 * 4 * 5 = 120
Print "Number of bytes used in array:", arraysize '120 * 4 = 480


See also