Question

I'm just starting with visual basic.

The first element in an array

i.e. dim y(9) as Integer

Will that have elements 0-8 or 1-9 in?

i.e. if I did

y(1) = 84

does that make the first or second element in the array equal to 84?

I've looked at a few online sources and I'm a bit confuzzled.

Thanks

Was it helpful?

Solution

The first position for arrays in VB.NET is zero; same rules apply to any in-built collection/function requiring indexing and to other .NET languages, like C#. On the other hand, "old" VB (VB6 and older or VBA) has zero as first index for arrays and one for in-built functions (this is somehow confusing for people coming from VB to VB.NET). What is common to all the VB versions (.NET and older ones) and different to other languages like C#, is that the number used when instantiating the array (9 in y(9)) does not indicate its size but its last index (the size of y(9) is 10).

In summary, your array includes 10 positions: from 0 to 9.

OTHER TIPS

The elements in the array start from 0 in VB.NET

A sample example from MSDN:

enter image description here

You may also find this interesting to read:- Why numbering should start at zero

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top