From Xojo Documentation

Revision as of 18:53, 19 November 2009 by WikiSysop (talk) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Returns the index of the last element in an array. Arrays are zero-based. Element zero many or not be used.


result=Ubound(array[,dimension]) OR


Changed 2007r4


Part Type Description
result Integer The index of the last element in the array specified.

If the passed array has no elements, result is set to -1.

array Array of any data type The array whose last element number you want.
dimension Integer Relevant only for multi-dimensional arrays. Used to specify the dimension for which you want the last element.

The first dimension is numbered 1. If passed -1, it will return the number of dimensions in the array. If passed a non-existent dimension, it will cause an OutOfBoundsException error.


The Ubound function is used to determine the last element of an array, but it can also be used to determine the size of an array. It may appear at first that the last element number and the size of the array are the same but in fact they are not. All arrays have a zero element. In some cases element zero is used and in other cases it is not. You will need to keep this in mind when using the Ubound function to determine the number of values you have in the array. If the array is zero-based, then element zero is used to store a value and you will have to add one to the value returned by the Ubound function to make up for it.


This example replaces each occurrence of X in an array with Y.

For i=0 to Ubound(Names)
If Names(i)="X" Then

The following example returns -1 because the newly-declared array has no elements:

Dim i() as Integer

The following example uses the alternative syntax to get the value:

Dim myArray(5) as Integer

See Also

Dim statement; Array, Join, Split functions; Append, IndexOf, Insert, Pop, Redim, Remove, Shuffle, Sort, Sortwith methods; ParamArray keyword.