UBound 函数 (Visual Basic)

UBound 函数 (Visual Basic)http msdn microsoft com zh cn vbrun 95b8f22f aspxVisualBa 语言参考 UBound 函数 VisualBasic 更新 2007 年 11 月返回 c information uboundcsdn

http://msdn.microsoft.com/zh-cn/vbrun/95b8f22f.aspx

 

Visual Basic 语言参考
UBound 函数 (Visual Basic)

更新:2007 年 11 月

返回数组的指示维度的最大可用下标。

Public Function UBound( _
   ByVal Array As System.Array, _ 
   Optional ByVal Rank As Integer = 1 _
) As Integer
Array

必选。任何数据类型的数组。要在其中查找维度的最大可能下标的数组。

Rank

可选。为 Integer。要返回的最大可能下标的维度。对第一维使用 1,对第二维使用 2,依此类推。如果省略 Rank,则假定为 1。

Integer。指定维度的下标可以包含的最大值。如果 Array 只有一个元素,UBound 返回 0。如果 Array 没有元素,例如如果它是零长度字符串,则 UBound 返回 -1。

异常类型

错误号

条件

ArgumentNullException

9

ArrayNothing

RankException

9

Rank 小于 1 或 Rank 大于 Array 的秩。

如果正在升级使用无结构错误处理的 Visual Basic 6.0 应用程序,请参见“错误号”一列。(您可以根据 Number 属性(Err 对象)比较错误号。)然而,如果可能,应当考虑用 Visual Basic 的结构化异常处理概述替换这种错误控制。

由于数组下标从 0 开始,因此维度的长度比该维度最大的可用下标大 1。

对于具有以下维度的数组,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。

下面的示例使用 UBound 函数确定数组的指示维度的最大可用下标。

Visual Basic
复制代码
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.

命名空间:Microsoft.VisualBasic

模块:Information

程序集:Visual Basic 运行库(在 Microsoft.VisualBasic.dll 中)

参考
LBound 函数 (Visual Basic)
Dim 语句 (Visual Basic)
ReDim 语句 (Visual Basic)
ArgumentException
RankException
编程小号
上一篇 2025-01-13 15:46
下一篇 2025-01-13 15:33

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/109113.html