数组长度= 100应该是0到99? [英] Array length = 100 should be from 0 to 99 ?

查看:104
本文介绍了数组长度= 100应该是0到99?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这是非常基本的,可能是转发,如果是的话我很抱歉。


问题是这个声明:


私人strMyArray(100)作为字符串


将创建一个长度为101的字符串数组,但是长度

应该只有100(0到99)。


VB.NET中是否有设置使数组看起来像VB6'数组?


谢谢。

Hi,

This is very basic, It may be a repost, if so I''m sorry.

The problem is that this declaration :

Private strMyArray(100) As String

will create an array of string with a length of 101, but the length
should be only of 100 (0 to 99).

Is there a setting in VB.NET to enable arrays to look like VB6''s array ?

Thank you.

推荐答案

如果你想要一个包含100个元素的数组,你可以将它调暗为MYARRAY(99)。你以上限(0为基础)来调暗

,而不是元素数量

" User" <顾*** @ guest.com>在消息新闻中写道:n9xOc.25
If you want an array of 100 elements you dim it as MyArray(99). You dim by
the upper bound (0 based), not the number of elements
"User" <gu***@guest.com> wrote in message news:n9xOc.25


T_6.24@edtnps89 ...
T_6.24@edtnps89...


这是非常基本的,这可能是转发,如果是的话我很抱歉。

问题是这个声明:

私人strMyArray(100)As String

将创建一个长度为101的字符串数组,但长度
应该只有100(0到99)。

VB.NET中是否有设置启用数组看起来像VB6的数组?

谢谢。
Hi,

This is very basic, It may be a repost, if so I''m sorry.

The problem is that this declaration :

Private strMyArray(100) As String

will create an array of string with a length of 101, but the length
should be only of 100 (0 to 99).

Is there a setting in VB.NET to enable arrays to look like VB6''s array ?

Thank you.



在VB6中,你可以维度一个包含下限和上限的数组。如果

只使用了上限,则下限为0或1

,具体取决于Option Base语句。在.Net中,数组总是具有较低的

界限为0.


因此,Dim strArray(100)的VB6声明可能导致100或101

元素取决于选项基础设置。


在VB.Net中,决定代替维度设置数字

元素。对于从VB迁移以保持

维度作为上限的开发人员会更好。因此,VB.Net将产生101个元素。


以下内容直接来自文档:


Visual Basic .NET更新声明数组边界提供

与其他编程语言中的数组的互操作性。


Visual Basic 6.0
在Visual Basic 6.0中,默认数组每个维度的下限

为0.您可以使用Option Base语句将其更改为1。您还可以使用
覆盖单个数组声明中的默认下限。

如果将默认值保留为0,则数组中的元素数相等

到上限加一。以下声明为数组保留了21个元素

重量:


昏暗权重(20)作为单个Visual Basic .NET

在Visual Basic .NET中,每个数组维度的下限为0,并且您不能将其声明为
。 Option Base语句不支持



您为声明中每个维指定的数字是上限
,并且初始元素计数等于上限加一。

前面示例中的声明为Weight保留了21个元素,

,下标为0到20。 br />

您还可以通过将其上限之一声明为-1来指定零长度数组,该数组不包含任何

元素。 />
" User" <顾*** @ guest.com>在消息新闻中写道:n9xOc.25
In VB6, you could dimension an array with both a lower and upper bound. If
you used only an upper bound, then the lower bound was either 0 or 1
depending on the Option Base statement. In .Net, arrays always have a lower
bound of 0.

So a VB6 declaration of Dim strArray(100) could result in 100 or 101
elements depending on the option base setting.

In VB.Net, it was decided that instead of the dimension setting the number
of elements. it would be better for developers migrating from VB to keep the
dimension as the upper bound. So VB.Net would result in 101 elements.

The following is straight from the documentation:

Visual Basic .NET updates the declaration of array bounds to provide
interoperability with arrays in other programming languages.

Visual Basic 6.0
In Visual Basic 6.0, the default lower bound of every dimension of an array
is 0. You can change this to 1 with the Option Base statement. You can also
override the default lower bound in individual array declarations.

If you leave the default at 0, the number of elements in the array is equal
to the upper bound plus one. The following declaration reserves 21 elements
for the array Weight:

Dim Weight(20) As Single Visual Basic .NET
In Visual Basic .NET, the lower bound of every array dimension is 0, and you
cannot declare it to be otherwise. The Option Base statement is not
supported.

The number you specify for each dimension in the declaration is the upper
bound, and the initial element count is equal to the upper bound plus one.
The declaration in the preceding example reserves 21 elements for Weight,
with subscripts 0 through 20.

You can also specify a zero-length array, which does not contain any
elements, by declaring one of its upper bounds to be -1.
"User" <gu***@guest.com> wrote in message news:n9xOc.25


这篇关于数组长度= 100应该是0到99?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆