Information.UBound(Array, Int32) Information.UBound(Array, Int32) Method


傳回所指示的陣列維度之可用的最高註標。Returns the highest available subscript for the indicated dimension of an array.

public static int UBound (Array Array, int Rank = 1);
static member UBound : Array * int -> int


Array Array

必要項。Required. 任何資料類型的陣列,Array of any data type. 您想在該陣列中找到維度的最高可能註標。The array in which you want to find the highest possible subscript of a dimension.

Int32 Int32

選擇性。Optional. Integer.Integer. 針對可能的最高註標,所要傳回的維度。The dimension for which the highest possible subscript is to be returned. 使用 1 表示第一個維度,2 表示第二個維度,依此類推。Use 1 for the first dimension, 2 for the second, and so on. 如果省略了 Rank,則假設為 1。If Rank is omitted, 1 is assumed.


Integer.Integer. 指定之維度的註標可以包含的最高值。The highest value the subscript for the specified dimension can contain. 如果 Array 只有一個元素,則 UBound 會傳回 0。If Array has only one element, UBound returns 0. 如果 Array 沒有任何項目,例如它是零長度字串,則 UBound 會傳回 -1。If Array has no elements, for example if it is a zero-length string, UBound returns -1.


ArrayNothingArray is Nothing.

Rank 小於 1 或 Rank 大於 Array 的陣序規範。Rank is less than 1, or Rank is greater than the rank of Array.


下列範例會使用UBound函式來判斷所指定維度的陣列的最大可用註標。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.


由於陣列註標起始為 0,就有一個維度的長度大於一比該維度的最大可用註標。Since array subscripts start at 0, the length of a dimension is greater by one than the highest available subscript for that dimension.

與下列維度中,陣列UBound傳回下表中的值:For an array with the following dimensions, UBound returns the values in the following table:

Dim a(100, 5, 4) As Byte  
呼叫 UBoundCall to UBound 傳回值Return value
UBound(a, 1) 100100
UBound(a, 2) 55
UBound(a, 3) 44

您可以使用UBound判斷總計的項目數的陣列,但是您必須調整其傳回至註標起始為 0 的事實的值。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. 下列範例會計算陣列的大小總計a在上述範例中: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)  

計算值total為 3030,也就是 101 * 6 * 5。The value calculated for total is 3030, which is 101 * 6 * 5.