Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Information.UBound Method

Returns the highest available subscript for the indicated dimension of an array.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
public static int UBound(
	Array Array,
	int Rank
)

Parameters

Array
Type: System.Array

Required. Array of any data type. The array in which you want to find the highest possible subscript of a dimension.

Rank
Type: System.Int32

Optional. Integer. The dimension for which the highest possible subscript is to be returned. Use 1 for the first dimension, 2 for the second, and so on. If Rank is omitted, 1 is assumed.

Return Value

Type: System.Int32
Integer. The highest value the subscript for the specified dimension can contain. If Array has only one element, UBound returns 0. If Array has no elements, for example if it is a zero-length string, UBound returns -1.
ExceptionCondition
ArgumentNullException

Array is Nothing.

RankException

Rank is less than 1, or Rank is greater than the rank of Array.

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

Return value

UBound(a, 1)

100

UBound(a, 2)

5

UBound(a, 3)

4

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.

Dim highest, bigArray(10, 15, 20), littleArray(6) As Integer
highest = UBound(bigArray, 1)
highest = UBound(bigArray, 3)
highest = UBound(littleArray)
' The three calls to UBound return 10, 20, and 6 respectively.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.