[Lesson 9] << [Contents] >> [Lesson 11]
10.1 The Concept of Array
In this lesson, we shall learn how to work with arrays in Visual Basic 2017. By definition, an array is a variable with a single name that represents many different items. To deal with a single item, we only need to declare one variable. However, in dealing with multiple items of similar type, we need to declare an array of variables.
For example, if we need to enter one hundred names, it is difficult to declare 100 different names. Besides, if we want to process those data that involves decision making, we might have to use hundreds of if…then statements, this is a waste of time and efforts. Therefore, 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. , makes declaring variables more two-dimensional
Example 10.2
An array in Visual Basic 2017 is a table of items that is made up of rows and columns. The way to reference an element in a one dimensional array is ArrayName(x), where x is the index or position number of the element. The way to reference an element in a two dimensional array is ArrayName(x,y) , where (x,y) is the index or position number of the element. Usually 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.
10.2 Dimension of an Array
In Visual Basic 2017, 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.
Table 10.1: One Dimensional Array
Student Name | Name(0) | Name(1) | Name(2) | Name(3) | Name(4) | Name(5) | Name(6) |
---|
Table 10.2: Two Dimensional Array
A two dimensional array is like a list of items or a table that consists of n row of items and m column of items. The following example is a 4×4 array.
Column1 | Column2 | Column3 | Column4 | |
---|---|---|---|---|
Row1 | Name(0,0) | Name(0,1) | Name(0,2) | Name(0,3) |
Row2 | Name(1,0) | Name(1,1) | Name(1,2) | Name(1,3) |
Row3 | Name(2,0) | Name(2,1) | Name(2,2) | Name(2,3) |
Row4 | Name(3,0) | Name(3,1) | Name(3,2) | Name(3,3) |
10.2 Declaring Arrays
In Visual Basic 2017 , 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 or module.
10.2.1 Declaring One Dimensional Array
The syntax to declare a one dimensional array is as follows:
Dim arrayName(n) as dataType
where n indicates the last index(or subscript) in the array. Please note that n does not indicate the number of elements in the array, it is one less than the number of elements (n-1) because the first element is always the zeroth element. The first element is arrayName(0), the second element is arrayName(1), the third element is arrayName(2) and so on. The number of elements in an array is also known as length, we can retrieve the length of an array using the syntax arrayName.length
For example,
Dim CusName(10) as String
will declare an array that consists of 11 elements starting from CusName(0) through to CusName(10)
To find out the length of the array, you can write the following code:
Example 10.1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim CusName(10) As String MsgBox(CusName.Length) End Sub
Running the program will produce a message box that displays the length of the array i.e 11, as shown in Figure 10.1
Figure 10.1
You might also declare an array with a non-zero starting index by initialize an index value other than zero, as follows:
Dim arrayname As DataType() arrayName=New String(){1,2,3,....,n)
This array will consists of n elements, starting with arrayName(1)
Example 10.2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim CusName As String() CusName = New String() {1, 2, 3} MsgBox(CusName.Length) End Sub
The message box will display the length as 3
Example 10.3
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim num As Integer Dim CusName(5) As String For num = 0 To 5 CusName(num) = InputBox("Enter the customer name", "Enter Name") ListBox1.Items.Add(CusName(num)) Next End Sub
This program will prompt the user to enter names in an input box for a total of 6 times and the names will be entered into a list box, as shown in Figure 10.2 and Figure 10.3
Figure 10.3
Figure 10.4
10.2.2 Declaring Two-Dimensional Array
The statement to declare a two-dimensional array is as follows:
Dim ArrayName(m,n) as dataType
where m and n indicate the last indices in the array. The number of elements or the length of the array is (m+1)x(n+1)
Example 10.4
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim CusName(5,6) As String MsgBox(CusName.Length) End Sub
Running the program and the message box will display a length of 42, as shown in Figure 10.5
Figure 10.5
Example 10.5
In this example, we want to summarize the first half-yearly sales volume for four products. Therefore, we declare a two-dimensional array as follows:
Dim saleVol(4, 6) As Integer
Besides that, we want to display the output in a table form. Therefore, we use a list box. We named the list box listVolume. The syntax to populate the list box is Listbox.Items.Add.
The Code
Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click Dim prod, mth As Integer ' prod is product and mth is month Dim saleVol(4, 6) As Integer Const j = 1 ListVolume.Items.Add(vbTab & "January" & vbTab & "February" & vbTab & "March" _ & vbTab & "Apr" & vbTab & "May" & vbTab & "June") ListVolume.Items.Add(vbTab & "____________________________________________") For prod = 1 To 4 For mth = 1 To 6 saleVol(prod, mth) = InputBox("Enter the sale volume for" & " " & "product" & " " & prod & " " & "month" & " " & mth) Next mth Next prod For i = 1 To 4 ListVolume.Items.Add("Product" & "" & i & vbTab & saleVol(i, j) & vbTab & saleVol(i, j + 1) & vbTab & saleVol(i, j + 2) _ & vbTab & saleVol(i, j + 3) & vbTab & saleVol(i, j + 4) & vbTab & saleVol(i, j + 5)) Next i End Sub
*Notice that we have omitted the elements saleVol(0,m), where m=0,1,2,3,4,5,6 and saleVol(n,0) where n=0,1,2,3,4. This is done for easy reading of the code.
The Output
Figure 10.6
16.3 Dynamic Array
So far we have learned how to define the number of elements in an array during design time. This type of array is known as a static array. However, the problem is sometimes we might not know how many data items we need to store during runtime. In this case, we need to use a dynamic array where the number of elements will be decided during runtime. In VB2017, the dynamic array can be resized when the program is executing. The first step in declaring a dynamic array is by using the Dim statement without specifying the dimension list, as follows:
Dim myArray()
Then at run time, we can specify the actual array size using the ReDim statement, as follows:
ReDim myArray(n)
* n =array size
You can also declare a two-dimensional array using ReDim statement, as follows:
ReDim myArray(n, m)
*mxn indicates the array size.
Example 10.6
In this example, we want to display the elements of an array in a list box. The size of the array will only be known during runtime. It demonstrates the creation of a dynamic array using the ReDim keyword.
The Code
Public Class Form1 Private Sub BtnDisplay_Click(sender As Object, e As EventArgs) Handles BtnDisplay.Click Dim myArray() As Integer Dim i, n As Integer n = InputBox("Enter the upper bound of array") ListBox1.Items.Clear() For i = 1 To n ReDim myArray(i) myArray(i) = i ^ 2 ListBox1.Items.Add(myArray(i)) Next End Sub End Class