473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Array length = 100 should be from 0 to 99 ?

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 22 '05 #1
8 2040
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 22 '05 #2
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 22 '05 #3
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 22 '05 #4
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 22 '05 #5
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 22 '05 #6
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 22 '05 #7
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 22 '05 #8
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 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
1
by: Samuel R. Neff | last post by:
Are there any differences between using Array.Length and Array.GetUpperBound(0) on a one-dimensional array? We have a team of developers and most people use Array.Length but one developer uses...
9
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void...
5
by: Stephen3776 | last post by:
I am doing an inventory control progam and trying to output a multiple array, I am getting an illegal conversion error java.lang.double !d. Can somebody tell me what I am doing wrong or if there is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.