An array is a collection of values of the same data type. The values in an array are called array elements. Array elements are accessed using a single name and an index number representing the position of the element within the array.
Arrays are used in a database application to handle data for processing. The picture given below shows how we can use arrays to fill the windows form controls like listbox, combobox, etc.
If you wish to know how to fill data in a windows form, read books listed on the home page.
An array is a standard structure for storing data in any programming language. Variables can hold single entities, such as one number, one date, or one string and arrays can hold sets of data of the same type (a set of numbers, a series of dates, and so on). An array has a name, and the values stored in it can be accessed by an index.
For example, we could use the variable 'age' to store a person´s age.
But what if we wanted to store the age of 20 employees? We could either declare 20 variables Age1, Age2, up to Age20 or we could declare an array with 20 elements. An array is similar to a variable:it has a name and multiple values. Each value is identified by an index (an integer value) that follows the array´s name in parentheses. Each different value in an array is an element of the array. If the array 'Ages' holds the ages of 20 employees, the element Ages(0) holds the age of the first employee, the element Ages(1) holds the age of the second employee, and so on up the element Ages(19).
The indexing of arrays in VB 2005 starts at zero.
As the Option statement is no longer valid in vb 2005, it is not possible to specify whether the indexing of the array would start at 0 or 1. All arrays must now start at index zero. An alternative is increase the dimensions of arrays by one and ignore the zeroth element.
In VB6 we could specify not only the dimensions of an array but also the index of the very first element, with a declaration like
Dim myArray(50 To 100) As Integer
This notation is not valid in VB 2005
Unlike simple variables, arrays must be declared with the Dim (or Public, or Private) statement followed by the name of the array and the index of the last element in the array in parentheses for example:
Dim Ages(19) As Integer
As discussed, Ages is the name of an array that holds 20 values (the ages of the 20 employees), with indices ranging from 0 to 19. Ages(0) is the first person´s age, Ages(1) the second person´s age, and so on. All we have to do is remember who corresponds to each age, but even this data can be handled by another array. To do this, we declare another array of 19 elements as follows:
Dim Names(19) As String
and then assign values to the elements of both arrays:
Ages (19) =45
This structure is more compact and more convenient than having to hard-code the names of
employees and their Ages in variables.
All elements in an array have the same data type. Of course, when the data type is an object, the
individual elements can contain different kinds of data (objects, strings, numbers, and so on).
As a better technique to store names and salaries together in an array, create a Structure and then
declare an array of this type. The following structure holds names and salaries:
Dim Name As String
Dim Age As Single
Insert this declaration in a form´s code file, outside any procedure. Then create an array of the
Dim Emps(19) As Employee
Each element in the Emps array exposes two fields, and we can assign values to them with statements like the following ones:
Emps(2).Name = "Ram"
Emps(2).Age = 32
The advantage of storing related pieces of information to a structure is that we can access all the items with a single index. The code is more compact, and we need not maintain multiple arrays.
Just as we can initialize variables in the same line where we declare them, we can initialize arrays too, with the following constructor:
Here´s an example that initializes an array of strings:
Dim names() As String ("john", "Sam","Hedric")
This statement is equivalent to the following statements, which declare an array with two elements and then set their values:
Dim names(2) As String
The number of elements in the curly brackets following the array´s declaration determines the dimensions of the array, and we can´t add new elements to the array without resizing it. If we need to resize the array in your vb code dynamically, we must use the ReDim statement, as described in the section "Dynamic Arrays," later in this article.
The first element of an array has index 0. The number that appears in parentheses in the Dim statement is one less than the array´s total capacity and is the array´s upper limit (or upper bound). The index of the last element of an array (its upper bound) is given by the function Ubound(), which accepts as argument the array´s name. For the array,
Dim myArray(19) As Integer
its upper bound is 19, and its capacity is 20 elements. The function UBound() is also exposed as a method of the Array object, and it´s the GetUpperBound method. It returns the same value as the Ubound() function. The GetLowerBound method returns the index of the array´s first element, which is always zero.
Let´s say we need an array to store 20 names. Declare it with the following statement:
Dim names (19) As String
The first element is names(0), and the last is names(19). If we execute the following statements, the values in bold will appear in the Output window:
Console .Wri teLl ne(names .GetLowerBound(0)) 0
Console .Wri teLi ne(names . GetupperBound(0)) 19
To assign a value to the first and last element of the names array, use the following statements:
names (0) ="First Name"
names(19) ="Last Name"
If we want to iterate through the array´s elements, use a loop like the following one:
Dim i As Integer, myArray(19) As Integer
For i 0 To myArray. GetUpperBound(0)
myArray(i) = i * 1000
The actual number of elements in an array is given by the expression,
myArray.GettUpperBound(0) + 1.
We can also use the array´s Length property to retrieve the count of elements. The following statement will print the number of elements in the array myArray on the Output window:
Arrays are one of the most improved areas of VB 2005. For years, programmers invested endless hours to write routines for sorting and searching arrays.
One-dimensional arrays, such as those presented so far, are good for storing long sequences of one- dimensional data (such as names). But how would we store a list of cities and their average temperatures in an array? Or names and ages, years and profits, or data with more than two dimensions, such as products, prices, and units in stock? In some situations we will want to store sequences of multidimensional data. We can store the same data more conveniently in an array of as many dimensions as needed.
A two-dimensional array has two indices. The first identifies the row (the order of the Account name in the array), and the second identifies the column (balance or age). To access the name and age of the third person in the two-dimensional array, use the following indices:
Ages(2, 0) ´the third name
Ages(2, 1) ´the third age
Sometimes we will not know how large an array to create. The earlier approach was to make it large enough to hold the maximum number of data. This will result in on an average, most of the array will be empty. To avoid this we can declare a dynamic array. The size of a dynamic array can vary during the execution of the software program.
With a dynamic array, we can discard the data and return the resources it occupied to the system.
To create a dynamic array, declare it as usual with the Dim statement, Public or Private but don´t specify its dimensions:
Dim DynArray As Integer
Later in the program, when we know how many elements we want to store in the array, use the ReDim statement to redimension the array, this time to its actual size. In the following example, UserCount is a user-entered value:
The ReDim statement can appear only in a procedure. Unlike the Dim statement, ReDim is executable, it forces the application to carry out an action at runtime. Dim statements aren´t executable, and they can appear outside procedures.
A dynamic array also can be redimensioned to multiple dimensions. Declare it with the Dim statement outside any procedure as follows
Dim Matrix() As Double
and then use the ReDim statement in a procedure to declare a three-dimensional array:
ReDim Matrix(9,9 , 9)
Note that the ReDim statement can´t change the type of the array that´s why the As clause is missing from the ReDim statement. Moreover, subsequent ReDim statements can change the bounds of the array Matrix but not the number of its dimensions. For example, we can´t use the statement ReDim Matrix(99, 99) later in your code. Once an array has been redimensioned once, its number of dimensions can´t change. In the preceding example, the Matrix array will remain three- dimensional through the course of the application.
The ReDim statement can be issued only from within a procedure. In addition, the array to be redimensioned must be visible from within the procedure that calls the ReDim statement.
THE PRESEVE KEYWORD
Each time we execute the ReDim statement, all the values currently stored in the array are lost. Visual Basic resets the values of the elements as if they were just declared. (It resets numeric elements to zero and String elements to empty strings.)
In most situations, when we resize an array, we no longer care about the data in it. We can, however, change the size of the array without losing its data. The ReDim statement recognizes the Preserve keyword, which forces it to resize the array without discarding the existing data. For example, we can enlarge an array by one element without losing the values of the existing elements by using the UBound() function as follows:
ReDim Preserve DynamicArray(UBound(DynArray) + 1)
If the array Dynamic Array held 12 elements, this statement would add one element to the array, the element Dynamic Array(12). The values of the elements with indices 0 through 11 wouldn´t change. The UBound() function returns the largest available index (the number of elements) in a one-dimensional array. Similarly, the Lbound () function returns the smallest index. If an array was declared with the statement
Dim Grades(49) As Integer
then the functions LBound(Grades) and UBound(Grades) would return the values 0 and 49.