VB.NET UBound 函数实际上做了什么,为什么 MSDN 文档似乎不准确?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29418248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:57:00  来源:igfitidea点击:

What does the VB.NET UBound function ACTUALLY do, and why does the MSDN documentation appear to be inaccurate?

.netvb.netvb6-migration

提问by AperioOculus

What does the UBound function actuallydo in VB.NET, and why doesn't the MSDN documentation appear to be accurate?

UBound 函数在 VB.NET 中实际做什么,为什么 MSDN 文档似乎不准确?

According to the MSDN documentation (here) the UBound function:

根据 MSDN 文档(此处)UBound 函数:

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

返回数组指定维度的最高可用下标。

More specifically:

进一步来说:

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.

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

But, in my testing the (and in some examples found in the documentation) the UBound function returns the lengthof an array instead of the highest available subscript:

但是,在我的测试中(以及在文档中的一些示例中),UBound 函数返回数组的长度而不是最高可用下标

Image from MSDN documentation on the UBound function

来自 MSDN 文档中关于 UBound 函数的图像

It's also important to note that UBound returns 1 for an Array containing one element, not 0 as the documentation states.

同样重要的是要注意,对于包含一个元素的数组,UBound 返回 1,而不是文档所述的 0。



In response to an answer:

回应一个回答:

I now see that when you declare an array in vb.net you are declaring the highest desired subscript NOT the length of the array as in C#.

我现在看到,当你在 vb.net 中声明一个数组时,你是在声明所需的最高下标,而不是 C# 中的数组长度。

Understanding that, I now understand why UBound was returning 1 instead of 0 for an array declared Dim c(1). Because this array will have the highest subscript of 1 thus giving it 2 elements. Further, to declare an array with only one element in vb.net it should be declared like this Dim b(0).

明白了这一点,我现在明白为什么 UBound 为声明的数组返回 1 而不是 0 Dim c(1)。因为这个数组的最高下标为 1,因此给它 2 个元素。此外,要在 vb.net 中声明一个只有一个元素的数组,它应该像这样声明Dim b(0)

enter image description here

在此处输入图片说明

回答by Joe

The documentation is accurate:

文档是准确的:

Your example:

你的例子:

Dim a(100,5,4)

is the same as (*)

是相同的 (*)

Dim a(0 To 100, 0 To 5, 0 To 4)

UBound(a,1)returns the highest available subscript of the first dimension, which is 100 (there are actually 101 elements, indexed from 0 to 100).

UBound(a,1)返回第一个维度的最高可用下标,即 100(实际上有 101 个元素,索引从 0 到 100)。

(*) Actually in VB6 and VBA you can override the default lower bound using the Option Base statement. But if you don't do this (and you shouldn't!), it will default to 0.

(*) 实际上在 VB6 和 VBA 中,您可以使用 Option Base 语句覆盖默认下限。但是如果你不这样做(你不应该这样做!),它将默认为 0。

Personally I always use 0 To Nwhen declaring VB arrays, to be absolutely explicit and not to rely on the Option Base setting. a(N)or a(0 To N)declare an array with N+1 elements, indexed from 0 To N, and the latter syntax makes this clearer IMHO.

我个人总是0 To N在声明 VB 数组时使用,绝对明确并且不依赖于 Option Base 设置。 a(N)或者a(0 To N)声明一个包含 N+1 个元素的数组,索引从 0 到 N,后一种语法使这更清楚恕我直言。

Note that in VBA/VB6 (from which VB.NET was derived) it is possible to use any lower bound, so you can also declare an array as, say:

请注意,在 VBA/VB6(从中派生 VB.NET)中,可以使用任何下限,因此您也可以将数组声明为,例如:

Dim a(500 to 600)

If you want to write code that can iterate over such arrays, you should always use:

如果要编写可以遍历此类数组的代码,则应始终使用:

For nIndex = LBound(a) To UBound(a)

It's also important to note that UBound returns 1 for an Array containing one element

同样重要的是要注意,对于包含一个元素的数组,UBound 返回 1

Not true - the following array has one element, and UBound(a)returns 0:

不正确 - 以下数组有一个元素,并UBound(a)返回 0:

Dim a(0) 

or

或者

Dim a(0 To 0)

The array

数组

Dim a(1)

is the same as:

是相同的:

Dim a(0 To 1)

and has two elements a(0)and a(1). LBoundreturns 0 and UBoundreturns 1, just as you'd expect.

并且有两个元素a(0)a(1)。 正如您所期望的那样,LBound返回 0 并UBound返回 1。

One final point: UBound(a) will return -1 for an empty array (no elements). AFAIK you can't create such an array with VB6/VBA code, but you can get one returned from code written in other languages, including VB.NET

最后一点: UBound(a) 将为空数组(无元素)返回 -1。AFAIK 你不能用 VB6/VBA 代码创建这样一个数组,但是你可以从用其他语言编写的代码返回一个,包括 VB.NET