By definition, an array is a list of variables with the same data type and name. When we work with a single item, we only need to use one variable. However, if we have a list of items which are of similar type to deal with, we need to declare an array of variables instead of using a variable for each item
For example, if we need to enter one hundred names, it is difficulty to declare 100 different names, this is a waste of time and efforts. So, instead of declaring one hundred different variables, we need to declare only one array. We differentiate each item in the array by using subscript, the index value of each item, for example name(1), name(2),name(3) .......etc. , which will make declaring variables streamline and much more systematic.
16.2 Dimension of an Array
An array can be one dimensional or multidimensional. One dimensional array is like a list of items or a table that consists of one row of items or one column of items.
A two dimensional array is a table of items that make up of rows and columns. The format for a one dimensional array is ArrayName(x), the format for a two dimensional array is ArrayName(x,y) and a three dimensional array is ArrayName(x,y,z) . Normally it is sufficient to use one dimensional and two dimensional array ,you only need to use higher dimensional arrays if you need to deal with more complex problems. Let me illustrate the the arrays with tables.
|Table 16.1. One dimensional Array|
Table 16.2 Two Dimensional Array
We can use Public or Dim statement to declare an array just as the way we declare a single variable. The Public statement declares an array that can be used throughout an application while the Dim statement declare an array that could be used only in a local procedure.
The general format to declare a one dimensional array is as follow:
Dim arrayName(subs) as dataType
where subs indicates the last subscript in the array.
Dim CusName(10) as String
will declare an array that consists of 10 elements if the statement Option Base 1 appear in the declaration area, starting from CusName(1) to CusName(10). Otherwise, there will be 11 elements in the array starting from CusName(0) through to CusName(10)
Dim Count(100 to 500) as Integer
declares an array that consists of the first element starting from Count(100)
and ends at Count(500)
The general format to declare a two dimensional array is as follow:
Dim ArrayName(Sub1,Sub2) as dataType
Dim StudentName(10,10) will declare a 10x10 table make up of 100 students' Names, starting with StudentName(1,1) and end with StudentName(10,10).
16.3 Sample Programs
Dim studentName(10) As String
Private Sub addName()
The program accepts data entry through an input box and displays the entries in the form itself. As you can see, this program will only allows a user to enter 10 names each time he click on the start button.
Dim studentName(10) As String
Dim num As Integer
Private Sub addName( )
The program accepts data entries through an InputBox and displays the items in a list box.
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: email@example.com