VB.NET UBound 函数实际上做了什么,为什么 MSDN 文档看起来不准确?
What does the VB.NET UBound function ACTUALLY do, and why does the MSDN documentation appear to be inaccurate?
UBound 函数 实际上 在 VB.NET 中做了什么,为什么 MSDN 文档看起来不准确?
根据 MSDN 文档 (here),UBound 函数:
Returns the highest available subscript for the indicated dimension of an array.
更具体地说:
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.
但是,在我的测试中(以及在文档中找到的一些示例中)UBound 函数 returns 数组的 length 而不是 最高可用下标:
同样重要的是要注意,对于包含一个元素的数组,UBound returns 1,而不是文档所述的 0。
回复一个回答:
我现在看到,当您在 vb.net 中声明数组时,您声明的是所需的最高下标,而不是 C# 中的数组长度。
明白了这一点,我现在明白了为什么 UBound 为声明的数组返回 1 而不是 0 Dim c(1)
。因为这个数组的最高下标为 1,所以它有 2 个元素。此外,要在 vb.net 中声明一个只有一个元素的数组,应该这样声明 Dim b(0)
.
文档准确:
你的例子:
Dim a(100,5,4)
与(*)
相同
Dim a(0 To 100, 0 To 5, 0 To 4)
UBound(a,1)
returns第一维的最高可用下标,为100(实际上有101个元素,索引从0到100)。
(*) 实际上,在 VB6 和 VBA 中,您可以使用 Option Base 语句覆盖默认下限。但是如果你不这样做(你不应该这样做!),它将默认为 0。
我个人总是在声明 VB 数组时使用 0 To N
,以绝对明确而不依赖于 Option Base 设置。 a(N)
或 a(0 To N)
声明一个包含 N+1 个元素的数组,索引从 0 到 N,后一种语法使这个更清晰恕我直言。
请注意,在 VBA/VB6(VB.NET 的来源)中可以使用任何下限,因此您也可以将数组声明为:
Dim a(500 to 600)
如果您想编写可以遍历此类数组的代码,您应该始终使用:
For nIndex = LBound(a) To UBound(a)
It's also important to note that UBound returns 1 for an Array containing one element
不正确 - 以下数组只有一个元素,并且 UBound(a)
returns 0:
Dim a(0)
或
Dim a(0 To 0)
数组
Dim a(1)
等同于:
Dim a(0 To 1)
并且有两个元素a(0)
和a(1)
。 LBound
returns 0 和 UBound
returns 1,正如您所期望的那样。
最后一点:UBound(a) 将 return -1 用于空数组(无元素)。据我所知,您无法使用 VB6/VBA 代码创建这样的数组,但您可以从其他语言编写的代码中获得一个 returned,包括 VB.NET
UBound 函数 实际上 在 VB.NET 中做了什么,为什么 MSDN 文档看起来不准确?
根据 MSDN 文档 (here),UBound 函数:
Returns the highest available subscript for the indicated dimension of an array.
更具体地说:
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.
但是,在我的测试中(以及在文档中找到的一些示例中)UBound 函数 returns 数组的 length 而不是 最高可用下标:
同样重要的是要注意,对于包含一个元素的数组,UBound returns 1,而不是文档所述的 0。
回复一个回答:
我现在看到,当您在 vb.net 中声明数组时,您声明的是所需的最高下标,而不是 C# 中的数组长度。
明白了这一点,我现在明白了为什么 UBound 为声明的数组返回 1 而不是 0 Dim c(1)
。因为这个数组的最高下标为 1,所以它有 2 个元素。此外,要在 vb.net 中声明一个只有一个元素的数组,应该这样声明 Dim b(0)
.
文档准确:
你的例子:
Dim a(100,5,4)
与(*)
相同Dim a(0 To 100, 0 To 5, 0 To 4)
UBound(a,1)
returns第一维的最高可用下标,为100(实际上有101个元素,索引从0到100)。
(*) 实际上,在 VB6 和 VBA 中,您可以使用 Option Base 语句覆盖默认下限。但是如果你不这样做(你不应该这样做!),它将默认为 0。
我个人总是在声明 VB 数组时使用 0 To N
,以绝对明确而不依赖于 Option Base 设置。 a(N)
或 a(0 To N)
声明一个包含 N+1 个元素的数组,索引从 0 到 N,后一种语法使这个更清晰恕我直言。
请注意,在 VBA/VB6(VB.NET 的来源)中可以使用任何下限,因此您也可以将数组声明为:
Dim a(500 to 600)
如果您想编写可以遍历此类数组的代码,您应该始终使用:
For nIndex = LBound(a) To UBound(a)
It's also important to note that UBound returns 1 for an Array containing one element
不正确 - 以下数组只有一个元素,并且 UBound(a)
returns 0:
Dim a(0)
或
Dim a(0 To 0)
数组
Dim a(1)
等同于:
Dim a(0 To 1)
并且有两个元素a(0)
和a(1)
。 LBound
returns 0 和 UBound
returns 1,正如您所期望的那样。
最后一点:UBound(a) 将 return -1 用于空数组(无元素)。据我所知,您无法使用 VB6/VBA 代码创建这样的数组,但您可以从其他语言编写的代码中获得一个 returned,包括 VB.NET