An array is a set of related pieces of data or values that is indexed. This articel describes how to write code that uses arrays in Visual Basic
Arrays are used by programmers to refer and to recall different values of the same class of data (in the example below I’ll be using countries, but it could be, names, addresses, test results or any other piece of information). They are basically lists, each item or row on the list is given a unique number called an index (or a subscript – depending on the programmer and the books they read). Arrays simplify the code needed, making it easier for a programmer to write a loop that can work powerfully with large numbers of elements. It is possible to not use arrays, and to declare each element as a variable, but the amount of coding this would entail would slow down a program immensely.
An array is a set of related pieces of data or values that is indexed; they are used to group and index to a series of values for recall within a program. Each of the values contained by an array is called an element.
An element is one piece of data, as a computer has no concept of what countries are, or the words we use to label, them a coder has to tell the computer that there will be a table or list of words referred to as “countries”. Once the space for the countries has been allowed for the code writer can then tell computer that England, Wales, Greece, Gorge and Qatar are all countries, and the countries are indexed or subscripted so that country 1 is England, country 2 is Wales and so on.
Types of Array
Visual Basic Arrays can be dynamic or fixed-sized. A fixed-sized array will always have the same number of elements during the execution of the program. It’s useful for static lists like the months in a year, or any other list that will be unchanging. Dynamic arrays can change during run time and use information imported from a file or manually entered while the program executes.
Arrays are also linear or multidimensional, so there are fixed-sized linear arrays, fixed-sized multidimensional arrays, and dynamic linear or dynamic multidimensional arrays. Linear arrays contain a single list of variables, such as the countries above. A multidimensional array uses more than one set of indexes or subscripts, 2-dimentional arrays can be thought of as a table with columns and rows. This could hold the countries name in one column, the phone code in a second, and a postal abbreviation a third column.
Array Declaration Statements
Arrays are declared in programs the same way as a variable is, they need a declaration statement. As with variables the place in the code, and the way they are declared will affect the scope that an array can be used in. To declare an array 4 pieces of information are needed; the name of the array, what data type it is, the number of dimensions in the array, and the number of elements.
A fixed-size array, storing the Countries England, Wales, Greece and Qatar could be declared:
Dim strCountries(4) As String
The “Dim” declares that there will be an array with the title “strCountries” and this array can hold 5 Countries. Visual Basic counts the index starting from 0, the spaces allocated by the programmer need to be minus 1 to allow for that. One number is in the parentheses so this is a one-dimensional or linear array.
Next the countries are indexed (or given the subscripts) 0 to 4.
strCountries(0) = "England"
strCountries(1) = "Wales"
strCountries(2) = "Greece" and so on.
An array index can’t be any longer than a Long Data Type, but the long data type has a value range of -9,223,372,036,854,775,808 through to 9,223,372,036,854,775,807 (9.2...E+18). There's probably enough wriggle room there for most projects.
To declare an array that two-dimensional array the parentheses would hold a second number split by a comma:
Dim strCountries(4,1) As String
This snippet declares an array called strCountries which could contain 5 countries and their 5 corresponding dialling codes; 5 rows, with two columns in each row.
Example of a Dynamic Array
Code to declare the linear array dynamically would read
Dim strCountries() As String
Leaving the parentheses empty creates a dynamic array, but it can’t contain any data because the computer hasn’t been told to keep any memory space for the elements.
The array needs to be redimensioned – redimensioning tells the computer to allocate a certain amount of memory space. To redimension the strCountries array to hold 5 Countries would read:
If a further country is added while the program is running more space will be needed. The ReDim command alone would write over the data already being stored so to add a 6th country (Ireland in this example) the programmer would use the “Preserve” command.
ReDim Preserve strCountries(5)
strCountries(5) = “Ireland”
Multidimensional Array Usage
Arrays in VB can have up to 32 dimensions, but it is unusual for code writers to use more than 3 (very useful for producing 3D images) or 4 (for calcuations using movement through space and time). More than four… well, then we’re into the field of mathematicians much cleverer than myself…