A standard structure for storing data in any programming language is the array. Whereas individual variables can hold single entities, such as one number, one date, or one string, 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, as does a variable, and the values stored in it can be accessed by an index. For example, you could use the variable Salary to store a person’s salary:
Salary = 34000
Code language: VB.NET (vbnet)
But what if you wanted to store the salaries of 16 employees? You could either declare 16 variables — Salary1, Salary2, and so on up to Salary16 — or declare an array with 16 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 is an element of the array. If the array Salaries holds the salaries of 16 employees, the element Salaries(0) holds the salary of the first employee, the element Salaries(1) holds the salary of the second employee, and so on up to the element Salaries(15).
Declaring Arrays
Unlike simple variables, arrays must be declared with the Dim (or Public) statement followed by the name of the array and the index of the last element in the array in parentheses— for example:
Dim Salary(15) As Integer
Code language: VB.NET (vbnet)
Salary is the name of an array that holds 16 values (the salaries of the 16 employees) with indices ranging from 0 to 15. Salary(0) is the first person’s salary, Salary(1) the second person’s salary, and so on. All you have to do is remember who corresponds to each salary, but even this data can be handled by another array. To do this, you’d declare another array of 16 elements:
Dim Names(15) As String
Then assign values to the elements of both arrays:
Names(0) = "Joe Doe"
Salary(0) = 34000
Names(1) = "Beth York"
Salary(1) = 62000
...
Names(15) = "Peter Smack"
Salary(15) = 10300
Code language: VB.NET (vbnet)
This structure is more compact and more convenient than having to hard-code the names of employees and their salaries in variables.
All elements in an array have the same data type. Of course, when the data type is Object, the individual elements can contain different kinds of data (objects, strings, numbers, and so on).
Arrays, like variables, are not limited to the basic data types. You can declare arrays that hold any type of data, including objects. The following array holds colors, which can be used later in the code as arguments to the various functions that draw shapes:
Dim colors(2) As Color
colors(0) = Color.BurlyWood
colors(1) = Color.AliceBlue
colors(2) = Color.Sienna
Code language: VB.NET (vbnet)
The Color class represents colors, and among the properties it exposes are the names of the colors it recognizes.
A better technique for storing names and salaries is to create a structure and then declare an array of this type. The following structure holds names and salaries:
Structure Employee
Dim Name As String
Dim Salary As Decimal
End Structure
Code language: VB.NET (vbnet)
Insert this declaration in a form’s code file, outside any procedure. Then create an array of the Employee type:
Dim Emps(15) As Employee
Code language: VB.NET (vbnet)
Each element in the Emps array exposes two fields, and you can assign values to them by using statements such as the following:
Emps(2).Name = "Beth York"
Emps(2).Salary = 62000
Code language: VB.NET (vbnet)
The advantage of using an array of structures instead of multiple arrays is that the related information will always be located under the same index. The code is more compact, and you need not maintain multiple arrays.