By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,234 Members | 1,877 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,234 IT Pros & Developers. It's quick & easy.

Array length = 100 should be from 0 to 99 ?

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
md
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...
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.

Nov 20 '05 #2

P: n/a
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$T_6.24@edtnps89...
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.

Nov 20 '05 #3

P: n/a
Thank you guys for your explanations,

Now I understand!

Roy Soltoff wrote:
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$T_6.24@edtnps89...
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.



Nov 20 '05 #4

P: n/a
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
Nov 20 '05 #5

P: n/a
Roy Soltoff <ro*****@CLEANmedicomp.com> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 20 '05 #6

P: n/a
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
Nov 20 '05 #7

P: n/a
md
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...
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.

Nov 20 '05 #8

P: n/a
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$T_6.24@edtnps89...
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.

Nov 20 '05 #9

P: n/a
Thank you guys for your explanations,

Now I understand!

Roy Soltoff wrote:
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$T_6.24@edtnps89...
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.



Nov 20 '05 #10

P: n/a
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
Nov 20 '05 #11

P: n/a
Roy Soltoff <ro*****@CLEANmedicomp.com> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 20 '05 #12

P: n/a
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
Nov 20 '05 #13

P: n/a
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Roy Soltoff <ro*****@CLEANmedicomp.com> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 20 '05 #14

P: n/a
Roy Soltoff <ro*****@CLEANmedicomp.com> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 20 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.