简体   繁体   English

为什么我的VB.NET数组具有额外的值?

[英]Why does my VB.NET array have extra values?

I declare my array 我声明我的数组

Dim A(N) As Integer

When I loop from 1 To N or 0 To N-1 there's an extra value at one end or the other. 当我从1 To N或从0 To N-1循环时,一端或另一端会有一个额外的值。

What's going on? 这是怎么回事?

(Intended to be a canonical question/answer.) (旨在作为规范的问题/答案。)

In VB.NET arrays almost always* have a lower bound of 0 and are declared mentioning their upper bound, not their length. 在VB.NET数组中,几乎总是*下界为0 ,并声明其上限而不是长度。

They did change the VB.NET syntax early on to allow you to remind yourself if needed: 他们确实在早期更改了VB.NET语法,以便您在需要时提醒自己:

Dim A(0 To N) As Integer

That 0 can't be anything else (such as a 1 or a constant zero). 0不能是其他任何东西(例如1或常数0)。

You can loop through all VB.NET array indexes using 您可以使用以下方法遍历所有VB.NET数组索引

For i = LBound(A) To UBound(A)

or, more simply, 或者,更简单地说,

For i = 0 To N

(*) You can use the .NET Framework to create arrays with other lower bounds, but you need to refer to them as Array and thus with late binding (and probably Option Strict Off ). (*)您可以使用.NET Framework创建具有其他下限的数组,但是您需要将它们称为Array并因此具有后期绑定(并且可能是Option Strict Off )。

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

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