473,769 Members | 3,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2061
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.co m> 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
interoperabilit y 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.co m> 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
interoperabilit y 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.co m> 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*****@CLEANm edicomp.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.CreateIns tance 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Roy Soltoff <ro*****@CLEANm edicomp.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.CreateIns tance 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 22 '05 #8
Roy Soltoff <ro*****@CLEANm edicomp.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.co m>
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
3266
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)) CType(local4, Short) = CType(src, Short)
35
6666
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
57861
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 GetUpperBound(0). I'd like the code to all be consistent but would like to know if there is any other reason I can provide to justify using only Array.Length instead of GetUpperBound(0). I read that Array.Length has special meaning to the...
9
5627
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void *array,int p,int r,int elemSize,int(*Compare)(const void *keyA,const void *keyB));
5
2507
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 another way? /* * Main.java * * Created on April 29, 2007, 6:57 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */
0
9416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9849
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8861
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7393
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.