array declarations

  • Thread starter Thread starter Jay
  • Start date Start date
J

Jay

Why is it that array declarations are different than expected (other
languages)

for example:

dim strArray(5) as String

really means that there are 6 elements and their names are 0, 1,2,3,4,5

I would expect the following:

dim strArray(5) as String

There are 5 elements and their names are 0, 1,2,3,4
 
Jay,

This is because VBNet can work with the "one" indexer and the "zero"
indexer. In my opininion are there as well other solutions for that, however
they did it like this consequently keeping the indexer 1 and not internally
set in those VBNet methods the one by -1 by instance.

I hope this gives an idea?

Cor

"Jay" <
...
 
Jay said:
Why is it that array declarations are different than expected (other
languages)

for example:

dim strArray(5) as String

really means that there are 6 elements and their names are 0, 1,2,3,4,5


Probably because VB used to allow declaring the lower bound:

Dim strA(10 To 20) As String

(You'll note that there are 11 elements there) VB also used to have an
Option Base such that either 0 or 1 could be used as the default:

Option Base 1
Dim strB(5) As String

In that case there were 5 elements : 1, 2, 3, 4, 5

Option Base 0
Dim strB(5) As String ' 6 elements there

Now because .Net's arrays are by default 0 based, then it should
follow that they mimic the old Option Base 0 functionality....

LFS
 
Jay said:
Why is it that array declarations are different than expected
(other languages)

for example:

dim strArray(5) as String

really means that there are 6 elements and their names are 0, 1,2,3,4,5

I would expect the following:

dim strArray(5) as String

There are 5 elements and their names are 0, 1,2,3,4

In Addition to the other replies: VB 2005 will support the 'Bla(0 To 100)'
syntax again, but only with zero bounds. I'll update my code to this syntax
when VB 2005 comes out to avoid ambiguity for the reader.
 
If you look at the documentation for VB.NET array declarations they state
that the number is the upper bound, not the length. The array is 0-based and
the Length property will return 6 in your example. The GetUpperBound method
will return 5.

ArraySizeInitializationModifier ::=
( UpperBoundList ) [ ArrayTypeModifiers ]
UpperBoundList::=
Expression |
UpperBoundList , Expression
 
Back
Top