PC Review


Reply
Thread Tools Rate Thread

Array length = 100 should be from 0 to 99 ?

 
 
User
Guest
Posts: n/a
 
      30th Jul 2004
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.


 
Reply With Quote
 
 
 
 
md
Guest
Posts: n/a
 
      30th Jul 2004
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" <(E-Mail Removed)> 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.
>
>



 
Reply With Quote
 
Roy Soltoff
Guest
Posts: n/a
 
      30th Jul 2004
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" <(E-Mail Removed)> 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.
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      30th Jul 2004
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


 
Reply With Quote
 
User
Guest
Posts: n/a
 
      30th Jul 2004
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" <(E-Mail Removed)> 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.
>>
>>

>
>
>


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      30th Jul 2004
Roy Soltoff <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      30th Jul 2004
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


 
Reply With Quote
 
Roy Soltoff
Guest
Posts: n/a
 
      2nd Aug 2004
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Roy Soltoff <(E-Mail Removed)> 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 - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      3rd Aug 2004
Roy Soltoff <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of an array =?Utf-8?B?YXByaWwyNw==?= Microsoft Excel Programming 3 19th Jun 2006 02:59 PM
Array.Length vs Array.GetUpperBound(0) -- any real differences? Samuel R. Neff Microsoft VB .NET 1 31st Mar 2005 05:41 PM
array = null or array.length = 0 Rene Microsoft C# .NET 1 31st Aug 2004 04:30 PM
Re: Array length S. Justin Gengo Microsoft ASP .NET 1 12th Jul 2004 06:30 PM
Re: Array length Rob T Microsoft ASP .NET 0 12th Jul 2004 04:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:36 PM.