Information.UBound(Array, Int32) 方法

定義

傳回所指示的陣列維度之可用的最高註標。

public static int UBound (Array Array, int Rank = 1);
static member UBound : Array * int -> int
Public Function UBound (Array As Array, Optional Rank As Integer = 1) As Integer

參數

Array
Array

必要。 任何資料類型的陣列, 您想在該陣列中找到維度的最高可能註標。

Rank
Int32

選擇性。 Integer. 針對可能的最高註標,所要傳回的維度。 使用 1 表示第一個維度,2 表示第二個維度,依此類推。 如果省略了 Rank,則假設為 1。

傳回

Integer. 指定之維度的註標可以包含的最高值。 如果 Array 只有一個元素,則 UBound 會傳回 0。 如果 Array 沒有任何項目,例如它是零長度字串,則 UBound 會傳回 -1。

例外狀況

ArrayNothing

Rank 小於 1 或 Rank 大於 Array 的陣序規範。

範例

下列範例會 UBound 使用 函式來判斷數位所指定維度的最高可用下標。

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 開始,所以維度的長度大於該維度的最高可用下標。

針對具有下列維度的陣列, UBound 傳回下表中的值:

Dim a(100, 5, 4) As Byte  
呼叫UBound 傳回值
UBound(a, 1) 100
UBound(a, 2) 5
UBound(a, 3) 4

您可以使用 UBound 來判斷陣列中的元素總數,但您必須調整其傳回的值,以考慮下標從 0 開始的事實。 下列範例會計算上述範例中陣列 a 的大小總計:

Dim total As Integer  
total = (UBound(A, 1) + 1) * (UBound(A, 2) + 1) * (UBound(A, 3) + 1)  

計算的值 total 是 3030,也就是 101 * 6 * 5。

適用於

另請參閱