Array starting at 1

I'm redim a array to start 1 and to go to 600, does anyone know how? i tryed using the "to", but never worked

ReDimPreserve Array(1 to 600)

[304 byte] By [DarkDarkDark] at [2007-12-24]
# 1

If I recall correctly and backed up by a article

Lower Boundary Is Always Zero

VB6 allowed you to have a nonzero lower boundary in your arrays in a couple of ways. First, you could declare an array to have a certain range. If you wanted an array to start with 1, you declared it like this:

Dim y(1 To 3) As Integer

This would create an array with three elements, indexed 1–3. If you didn't like this method, you could use Option Base, which allowed you to set the default lower boundary to either 0 (the default) or 1.

VB.NET removes those two options from you. You cannot use the "1 to x" syntax, and Option Base is no longer supported. In fact, because the lower boundary of the array is always 0, the Lbound function is no longer supported.

So you are somewhat restricted to creating an array of 600 elements and not using element 0 or creating a 599 element array and started at element 0. Either way you get 00 elements in your application.

Public Class Form1
Dim Array1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ReDim Preserve Array1(600)
End Sub
End Class

spotty at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

In general using the 0 based arrays is recommended - VB (and for that matter C# afaik) doesn't support creating non 0 based arrays. If you really, really want to do so, you can use the array class to create an instance that isn't 0 based:

Dim a As Array = System.Array.CreateInstance(GetType(Integer), New Integer() {600}, New Integer() {1})

Note that this array will not be strongly typed, so it will require several explicit conversions if option strict is on.

AlexMoura at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
the question was answered before in another topic :-)
ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

That article is out of date - you can now (as of 2005) use the "0 to x" syntax again, although you must specify 0 as the lower bound. The speculation is that this is allowed to either make VB6 developers feel more comfortable or that a future version of VB will allow non-zero lower bounds again.

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: VB to Python converter

DavidAnton at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

So the premise of the article is correct - just additional syntax has been added to make VB6 feel more comfortable.

Either way non-zero based arrays is not support in .NET 1.0, 1.1 or 2.0 and I havent seen anything to indicate that its about to make a reappearance in any of the current .NET development - so basically you cant create non zero based arrays - but now have a choice of syntax (although the older VB 0 to x is supported - the lower bound has to be 0 ) so you are simply creating unnecessary additional verbosity in an already verbose language.

spotty at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6
Actually, if I recall correctly, the reason why (0 to x) syntax is now allowed is that for a large ammount of cases it reduces the work needed to upgrade to .net.
AlexMoura at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...