Visual Basic Arrays
In Understanding Visual Basic Variable & Constant Types and Declaring Visual Basic Variables and Constants we looked at storing data (such as numbers, strings and boolean true or false values) in memory locations known as variables and constants. The variable types covered in those chapters were useful for storing one value per variable. Often, however, it is necessary to group together multiple variables into a self contained object. This is where the concept of Visual Basic Arrays comes in.
What is a Visual Basic Array
A Visual Basic array is an object that contains a specified number of variables of teh same type. Once you have grouped all the variable values into the array you can then perform tasks like sorting the array items into alphabetical or numerical order, accessing and changing the value assigned to each array item, and passing the group of items as a parameter to a Visual Basic Function or Subroutine (see Visual Basic Modules and Procedures) by passing just the array object.
In this chapter we will look in detail at how to create and manipulate both single and multidimensional Visual Basic arrays.
Each value in an array is known as an array element. The pointer to the location of a specific element in an array is called an index.
How to Declare a Visual Basic Array
An array is declared using the Dim keyword. The syntax is as follows:
Dim arrayname(size) As dataType
The arrayname is the name by which the arrray will referenced in code. The size value in parentheses defines the maximum size of the array. It is important to note that arrays elements are numbered starting from 0, so an array of size 10 actually holds 11 elements (indexed as 0 though to 10).
Finally, the dataType value defines the type of variable the array will hold.
The following Visual Basic code excerpt declares an array capable of containing 6 String variables:
Dim strColors(5) As String
Initializing a Visual Basic Array
As with other variable types, an array can be initialized during the declaration. This is achieved by encapsulating comma separated element values in braces ({}).
The following example initializes our colors array:
Dim strColors(5) As String = {"Red", "Green", "Blue", "Indigo", "Violet", "Yellow" }
Similarly, an Integer array would be initialized as follows:
Dim intNumbers(4) As Integer = { 12, 43, 33, 45, 63 }
Assigning Values to Individual Array Elements
Once an array has been declared it is often necessary to change the value of a particular array element. This is performed by referencing the array and the index of the element to be changed (keeping in mind that the first element of an array is index position 0).
For example, to change the value of the first element of a String array:
strColors(0) = "Orange"
Similarly, to change the second value of an Integer array:
intNumbers(1) = 25
A For loop can also be used to assign multiple values to an array. The following code excerpt loops through the elements of an array assigning the current value of the loop counter to the corresponding array index:
Dim intNumbers(10) As Integer Dim intCount As Integer For intCount = 0 To 10 intNumbers(intCount) = intCount Next intCount
Accessing Array Element Values
Accessing an element in a Visual Basic array is similar to assigning a value to an array element. All that is required is the array name and the index of the element to be accessed. The following Visual Basic code excerpt displays MessageBoxes containing the string value the first and seconds elements in the strColors array:
Dim strColors(5) As String = {"Red", "Green", "Blue", "Indigo", "Violet", "Yellow" } MessageBox.Show(strColors(0)) MessageBox.Show(strColors(1))
A For loop can also be used to iterate through each element of an array:
Dim strColors(5) As String = {"Red", "Green", "Blue", "Indigo", "Violet", "Yellow" } Dim intCount As Integer For intCount = 0 To 10 MessageBox.Show(strColors(intCount)) Next intCount
Finding the Size of an Array
The size of an array (i.e the number of elements contained in the array) can be obtained by accessing the Length property of the specific array.
For example, to find out the size of the strColors array:
Dim strColors(5) As String = {"Red", "Green", "Blue", "Indigo", "Violet", "Yellow" } Dim intArraySize As Integer intArraySize = strColors.Length