Groups | Blog | Home
all groups > dotnet general > july 2004 >

dotnet general : Array length = 100 should be from 0 to 99 ?


md
7/30/2004 3:24:38 PM
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


[quoted text, click to view]

Roy Soltoff
7/30/2004 3:37:52 PM
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.


[quoted text, click to view]

User
7/30/2004 7:17:39 PM
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.

User
7/30/2004 7:44:17 PM
Thank you guys for your explanations,

Now I understand!



[quoted text, click to view]
Jon Skeet [C# MVP]
7/30/2004 8:45:23 PM
[quoted text, click to view]

No they don't. See the various Array.CreateInstance overloads for ways
to create arrays with non-zero lower bounds. I wouldn't suggest doing
it though - the performance is significantly degraded (IIRC) and most
people will assume that arrays have a lower bound of zero.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Cor Ligthert
7/30/2004 9:42:16 PM
Hi User,

You can use in VBNet classic VB methods and use methods who are equal as the
methods from the other languages.

The classic VB methods are indexing from 1 to 100 as in your sample while
the other languages are indexing from 0 to 99.

(And you can use them mixed up)

So both are possible with VBNet. I would as well not see another possibility
than design it in this way as it is.

I hope this gives an idea?

Cor

Cor Ligthert
7/30/2004 9:56:40 PM
Hi Jon,

I did not see it was in General as well, when I has seen it I had written my
message in another way. More about starting counting at one because we have
ten fingers on our hands with wich the first humans on earth starting
counting with the first finger as one and the last as ten.

However you know those message from me probably already.

(I do not use it as you know, however I would have found it a better method
when I had done that from the first moment)

:-)

Cor

Roy Soltoff
8/2/2004 4:12:54 PM
That's kind of interesting as the Microsoft documentation stated, "In Visual
Basic .NET, the lower bound of every array dimension is 0, and you cannot
declare it to be otherwise.". That's verbatim from the .Net section of the
MSDN DVD. However, I do ssee an overloaded array constructor listed in the
docs that indeed allows you to set the lower and upper bounds of a
multi-dimensioned array. But looking over those constructors, none allow you
to set the lower bound of a singly-dimensioned array.

[quoted text, click to view]

Jon Skeet [C# MVP]
8/3/2004 5:50:28 AM
[quoted text, click to view]

There's nothing to stop you from using the constructor which can create
a multi-dimensional array to create a single-dimension array - just
pass a single length and a single lower bound.

Now, things get slightly odd when you differentiate between a .NET
array and a VB.NET array. I don't know about VB.NET, but certainly in
C# (current implementation) you can use the normal array features of
the language for either a single-dimensional array with a zero lower
bound, or for a multi-dimensional array with any lower bounds - but
*not* a single-dimensional array with a non-zero lower bound. When you
try to cast the Array to (say) object[] you get an exception.

So it really depends on how you read the docs - if you think of
"array" as "object()" or whatever, i.e. the *language* concept of the
array, then the docs may be correct. If you think of "array" as
"instance of System.Array" then they're not.

(I don't think I'd have corrected your earlier post if it had said
"VB.NET array" - but as it said ".Net array" I thought it was fair game
:)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
AddThis Social Bookmark Button