Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Nov 28, 2001

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 dimensions
Dim sArray(,) As String = CType(myArray, _
	String(,))

Dim i As Int32
For 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.
Bill McCarthy
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date