Arrays

Declaring Arrays

All arrays in VB have to be declared. The declaration tells VB the name of the array, how many items it can store, and what sort of data it can store. If you declare the array in the general declarations section of a form, the array is visible to all procedures and functions in that form. If you declare the array as Public in a Module it will be visible to every procedure and function in your project. To declare an array use the following syntax:

Dim ArrayName(LowerValue To HigherValue) As DataType

Dim specifies the scope of the array. If you declare it in a Form, use Dim or Private, if you declare it in a Module and you want every procedure to access it, declare it as Public. If you declare the array in a procedure use Dim. ArrayName is the name of the array. LowerValue is the first element in the array. HigherValue is the last element in the array. DataType is a valid datatype (ie String)/ You don't have to specify the LowerValue, but if you don't, the array will start from 0. For example,

Dim sTestArray(0 To 10) As String

is the same as

Dim sTestArray(10) As String

The code below declares nArray, with 1 as the first element, 30 as the last element, all of which stores Integers.

Dim nArray( 1 To 30 ) As Integer

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.

“In theory, theory and practice are the same. In practice, they're not.”