In this post, am going to teach you What is an Array?, How to Declare and Initialize an Array, Fixed-Size Arrays, Dynamic Arrays, Retrieving Array's Contents, Adding New Elements to an Array, Erasing an Array
An array is a data structure used to hold group of elements of the same data type. When the elements are stored, they are stored at sequential order with the first element having index 0 and the last element at index n-1, where n is the total number of elements in the array.
Array declaration simply means declaring an array to store data. Array is declared in Visual Basic using the Dim statement.
For instance
Dim myArray() As String
In the above instance, we have defined an array with the name myArray, and it should hold all string elements of data type.
Remember, array is the collection of relation related items. The array declared above is used for storing string values, we can also declare array that holds only integer values. Below is how to;
Dim myArray() As Integer
We can initialize arrays at the time of their declaration. For example:
Dim carsArray() As String = {"Toyota", "Camri", "Volvo"}
We have just declared the array "carsArray and added 3 cars elements to it.
In case of integer values, you can as well declare and initialize as follows;
Dim nums() As String = {1, 2, 4}
We have created an array named nums and added 3 numbers to it.
A fixed-size array is an array that is used to store a fixed number of items. Simply means, the number of elements that will be hold by array is specified during its declaration.
Take for instance you want to declare array that holds only 4 items. You can declare a fixed array as follows
Dim cars(0 to 3) As String
'and it can be initialized as follows
cars(0) = "Volvo"
cars(1) = "Toyota"
cars(2) = "Lexus"
cars(3) = "Range Rover"
In case you are wondering why we start assigning values from index of 0, array always start its count at 0.
Dynamic arrays are used to hold array elements which size is subject to grow at any time.
This means that new elements can be added to the array at any time. To demonstrate this, let us first define an array of strings:
Dim cars() As String
In the above, we have declared a string array named cars. Now let us assume that you want to add three elements to the array as well as giving room for resizing the array. Then ReDim statement is what you need. See below
ReDim nums(3)
cars(0) = "Volvo"
cars(1) = "Toyota"
cars(2) = "Lexus"
In the above, we have just stored three car products in the array, what about if need arises that we need to add another two car products? Then the solution is to use the "Array Preserve statement". See below for demonstration
ReDim Preserve cars(5)
cars(3) = "Range Rover"
cars(4) = "Land Cruiser"
Study the above example very well and you see ReDim Preserve cars(5). The 5 here means that we have redeclare the cars array to be size of 5 while using the Preserve keyword to retain the previous items stored in the array.
Retrieving the elements means accessing the array elements. To access an array element, we use its index. Let us demonstrate this using an example.
Sub carsArray()
Dim cars(4) As String
cars(0) = "Volvo"
cars(1) = "Toyota"
cars(2) = "Lexus"
cars(3) = "Range Rover"
End Sub
We have created array of cars above, now let us access all the elements.
Note, the array is been declared as general variable so that it can be access anywhere from the program
Sub ReadArray()
For i = cars.Items.Count - 1
MsgBox(cars(i))
'To add the array elements to a ListBox, just use
ListBox1.Items.Add(cars(i))
Next
End Sub
When array is created, it occupies memory space. A good performance recommendation is to clean up the memory after used.
And To free up the system memory space, you have to call the Erase statement followed by the name of the array. For instance:
Dim cars(4) As String
cars(0) = "Volvo"
cars(1) = "Toyota"
cars(2) = "Lexus"
cars(3) = "Range Rover"
Erase cars
We have declared an array of cars and added 4 elements to it. The Erase cars statement will erase the array.