devxlogo

Arrays With Non-Zero Lower Bounds

Arrays With Non-Zero Lower Bounds

In VB.NET, you can use the System.Array class to create an array with non-zero lower bounds. To do this, use the System.Array.CreateInstance method: Array.CreateInstance(Type, Lengths(), LowerBounds( ) ).If the array you create has two or more dimensions, you can cast it to a normal array. This example creates an array of strings equivalent to Dim sArray(5 To 14, 8 To 27):

 Dim Lengths() As Int32 = {10, 20}Dim LowerBounds() As Int32 = {5, 8}Dim myArray As Array = _	Array.CreateInstance(GetType(String), _	Lengths, LowerBounds)' have to declare the array with the correct' number of dimensionsDim sArray(,) As String = CType(myArray, _	String(,))Dim i As Int32For i = 0 To sArray.Rank - 1	Console.WriteLine _	("dimension {0} , LowerBound = {1}, _		UpperBound = {2}", _	i, sArray.GetLowerBound(i), _	sArray.GetUpperBound(i))Next

Note: You cannot cast to single dimension arrays because VB.NET creates them as vectors.

devx-admin

Share the Post: