简体   繁体   English

Visual Basic中数组的大小?

[英]Size of array in Visual Basic?

I've tried this code in VB: 我已经在VB中尝试了以下代码:

Dim a(1) As Byte
Console.WriteLine(a.Length)

The output is "2". 输出为“ 2”。 Anyone any idea why? 有人知道为什么吗?

If you are used to C/C++/C# languages you are used that when declaring an array to initialize it with the number of elements in the array. 如果习惯于使用C / C ++ / C#语言,则在声明数组以使用数组中的元素数对其进行初始化时会使用该语言。

C# : byte a[] = new byte[1]

will declare a byte array with 1 element (upperBound = 0) 将声明一个带有1个元素的字节数组(upperBound = 0)

The behavior is different in VB where, when declaring an array the parameter used in initialization represents the UpperBound of the array. VB中的行为不同,在VB中,声明数组时,初始化中使用的参数表示数组的UpperBound

VB.NET: Dim a(1) As Byte

will declare a byte array with 2 elements (upperBound = 1) 将声明一个包含2个元素的字节数组(upperBound = 1)

In Visual Basic, the size of an array is declared with the array's upper bound , where most languages declare the size of an array by specifying the number of elements in the array. 在Visual Basic中,使用数组的上限声明数组的大小,其中大多数语言通过指定数组中元素数量来声明数组的大小。 If you aren't aware of this, then your Visual Basic arrays end up being 1 element longer than you expected: 如果您不知道这一点,那么您的Visual Basic数组最终比预期长1个元素:

VB.NET: VB.NET:

 Dim a(1) as Byte ' under the hood, translated to byte[2] 
 Console.WriteLine("{0}", a.Length) ' output 2

 a(0) = 7 ' No error, element exists
 a(1) = 7 ' No error, element exists, array length is 2
 a(a.Length) = 7  ' error: Index was outside the bounds of the array.

C#: C#:

 byte[] a = new byte[1];
 Console.WriteLine("{0}", a.Length); // output 1

 a[0] = 7 // No error, element exists
 a[1] = 7 // error:  Index was outside of bounds of the array. (because array length is 1)
 a[a.Length] = 7; // error: Index was outside the bounds of the array.

The reason why Microsoft designed VB.NET to size arrays based on upper bound rather than array length is to make it easier to port code from VB6 to VB.NET. Microsoft之所以设计VB.NET以便根据上限而不是数组长度来调整数组大小,是为了使将代码从VB6移植到VB.NET更加容易。 The initial index of a VB6 array is 1, unless you declare Option Base 0. It was common to loop through an array of size N using For i = 1 To N . 除非您声明Option Base 0,否则VB6数组的初始索引为1。通常使用For i = 1 To N遍历大小为N的数组。 By designing VB.NET to interpret an array's sizing argument as an upper bound rather than the number of elements in the array, old VB6 code that looped from 1 to N could be ported directly to VB.NET. 通过设计VB.NET将数组的大小调整参数解释为上限而不是数组中元素的数量,可以将循环从1到N的旧VB6代码直接移植到VB.NET。 The array in VB.NET will have one extra element compared to what the array had in VB6 (the element at index 0) but otherwise behaves as it did in VB6. 与VB6中的数组(索引为0的元素)相比,VB.NET中的数组将具有一个额外的元素,但其行为与VB6中的相同。

You'll sometimes see people claim that Visual Basic creates a "wasted" element. 有时您会看到人们声称Visual Basic创建了一个“浪费”元素。 This is only true when porting legacy VB6 code which didn't expect an element at index 0. When writing new code, you just have to remember what the sizing parameter means (upper bound, not element count), and declare your arrays accordingly. 仅当移植不希望索引索引为0的旧版VB6代码时才如此。编写新代码时,您只需要记住sizing参数的含义(上限,而不是元素计数),并相应地声明数组即可。 Just reduce your sizing parameters by one compared to what you'd see in C#. 与在C#中看到的相比,只需将大小调整参数减少一个即可。 The resulting array will have elements from a(0) to a(a.Length-1) , just like a C# array. 结果数组将具有从a(0)a(a.Length-1)元素,就像C#数组一样。

Array starts from position 0. You are defining two positions. 数组从位置0开始。您要定义两个位置。

If you want only 1 position, then: 如果只想要1个职位,则:

Dim a(0) As Byte

and you will get a.Length as 1. 您将获得a.Length为1。

Dimension Length The index of each dimension is 0-based, which means it ranges from 0 through its upper bound. 尺寸长度每个尺寸的索引都是从0开始的,这意味着它的范围是从0到上限。 Therefore, the length of a given dimension is greater by 1 than the declared upper bound for that dimension. 因此,给定尺寸的长度比该尺寸的声明上限大1。

Array Size in Visual Basic Visual Basic中的数组大小

The previous answers each have pieces of the correct answer, but not the full correct answer. 先前的答案每个都有正确答案的一部分,但没有完整的正确答案。 When you declare an array (Like with your code: Dim a(1) As Byte) the number you put in the array declaration (in this case, 1) is NOT a declaration of how many entries in the array, it is a declaration of the upper boundary of the array. 当您声明一个数组(如您的代码:Dim a(1)As Byte)时,您在数组声明中输入的数字(在本例中为1)不是数组中有多少个条目的声明,它是一个声明数组的上边界

So, in your declaration, you're creating an array with 2 entries: a(0) and a(1) 因此,在声明中,您要创建一个包含2个条目的数组:a(0)和a(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM