UBound Function (Visual Basic)
Returns the highest available subscript for the indicated dimension of an array.
Public Function UBound( _ ByVal Array As System.Array, _ Optional ByVal Rank As Integer = 1 _ ) As Integer
Array is Nothing
Rank is less than 1 or Rank is greater than the rank of Array.
See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.
Since array subscripts start at 0, the length of a dimension is greater by one than the highest available subscript for that dimension.
For an array with the following dimensions, UBound returns the values in the following table:
Dim a(100, 5, 4) As Byte
Call to UBound
You can use UBound to determine the total number of elements in an array, but you must adjust the value it returns to account for the fact that the subscripts start at 0. The following example calculates the total size of the array a in the preceding example:
Dim total As Integer total = (UBound(A, 1) + 1) * (UBound(A, 2) + 1) * (UBound(A, 3) + 1)
The value calculated for total is 3030, which is 101 * 6 * 5.
The following example uses the UBound function to determine the highest available subscript for the indicated dimension of an array.
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)