Arrays

Getting the size of the array

Sometimes, you will need to find the upper and lower limits of an array. To do this you can use the LBound and UBound functions. This is especially useful if you have used the ReDim statment (see dynamic arrays). To find the upper bound of an array (ie the ... To number part) use the UBound Function, using the following syntax:

UBound(ArrayName, Dimension)

ArrayName is the name of your array. Dimension is an optional integer representing the dimension number for use when using multidimensional arrays (discussed later in this tutorial). To find the lower bound of an array (ie the number To ... part of the array) use the LBound function, using the following syntax:

LBound(ArrayName, Dimension)

ArrayName is the name of your array. Dimension is an optional integer representing the dimension for use when using multiple dimensional arrays. The following code fills a list box with the values of all the items in gArray.

' Get the lower and upper values of gArray
LowerVal = LBound(gArray)
UpperVal = UBound(gArray)
For i = LowerVal To UpperVal
    ' Add a new item to the list, containing the
    ' next item in the array
    List1.AddItem gArray(i)
Next

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Linux is only free if your time has no value” - Jamie Zawinski