Transcript Lecture 9

Arrays in Visual Basic
Week 9
CM30104-1
What is an array ?
An array is a data structure that enables us to
store a list of values that can be thought of as a
table
Enables us to reference several data values by one
variable name
Types of Arrays
There are 2 types of arrays in VB :

Data Arrays

Control Arrays
Two types of Arrays in VB
Data Arrays

Primarily used to store data which is related in
some way and of the same type, e.g:
 student grades for a class of twenty students
 weather details for a series of places.
Control Arrays

A mechanism for duplicating controls and
allowing the same event coding to be triggered
by an action to any of the elements of the
control array.
Data arrays ~ example
iNos
iNos (3)
Array name
42
1
31
2
98
3
4
5
Coding to reference
each element
Array index
Array elements
Array Declaration
Arrays are declared in the same way as for
variables.
Dim iNos (1 To 5) As Integer
or :
Dim iNos (5) As Integer
When no start number specified
array index begins at 0
1
42
2
98
3
21
4
77
5
30
0
42
1
98
2
21
3
77
4
30
An example -
Finding largest of a
sequence of numbers
Using several variables … iNum1, iNum2, iNum3 etc
and a complicated Nested If ………….
If (iNum1 > iNum2) And (iNum1 > iNum2) And …
… And (iNum1 > iNum5) Then
iLargest = iNum1
Else
If (iNum2 > iNum1) And (iNum2 > iNum3) ……
…….. etc
An example -
Finding largest of a
sequence of numbers
Using a For loop and one variable, iNum
iLargest = 0
For iLoopCount = 1 To 5
iNum = InputBox (“Enter next number”)
If iNum > iLargest Then
iLargest = iNum
End If
Next iLoopCount
Give iLargest an initial
very small value
Finding largest of a sequence of numbers using an Array
Dim iNos (1 To 5) as Integer
iLargest = 0
For iCount = 1 To 5
iNos (iCount) = InputBox (“Enter next number”)
If iNos (iCount) > iLargest Then
iLargest = iNos (iCount)
End If
Next iCount
iNos
iLargest
45
132
132
2
71
94
Finding largest of a sequence of marks & also
storing names associated with each mark
42
Fred
iHighestMark
98
98
Sue
21
Bill
Sue
77
30
iMarks(1 To 5)
Amy
Jonathon
sNames(1 To 5)
sBestStudent
Finding largest of a sequence of marks & also
storing names associated with each mark
42
98
Fred
Sue
21
Bill
77
Amy
30
Jonathon
iMarks(1 To 5)
sNames(1 To 5)
Fill the arrays first …
For iLoop = 1 To 5
sNames(iLoop) = ….
iMarks(iLoop) = ….
Next iLoop
then have other loops to
process the data in the
arrays
Arrays have many advantages –
e.g, we can then sort & list students in order
ORDER OF INPUT
ARRAYS AFTER SORTING
42
Fred
98
Sue
98
Sue
77
Amy
21
Bill
42
Fred
77
Amy
30
Jonathon
30
Jonathon
21
Bill
Example coding
‘fill arrays
For iCount = 1 to 5
sNames(iCount) = InputBox(“Name”)
iMarks(iCount) = InputBox(“Mark”)
Next iCount
‘display to listboxes
For iCount = 1 to 5
lstNames.AddItem sNames(iCount)
lstMarks.AddItem iMarks(iCount)
Next iCount
42
Fred
98
Sue
21
Bill
77
Amy
30
Jonathon
Payroll example
• Using a form like
this to:
• Input name & salary
• Store data in an
array
Name
Control
Properties
txtName
Text Box
Text = “”
txtSalary
Text Box
Text = “”
lblPayroll
Label
Caption = “”
cmdEmployee
Command
Button
Caption = “Add
Employee”
scrEmployee
Vertical Scroll
Bar
Min = 0; Max = 9
• Scroll through
previous data
entered
Payroll example
• Scroll bar set at
design stage to
• min = 0
• max = 9
• Data stored in 2
arrays:
sName (10) As String
sSalary (10) As Currency
sName
Fred Bloggs
John Smith
Jim Brown
Dave Boss
cSalary
0
1
2
3
4
5
6
7
8
9
10,200
14,000
8,250
25,600
Payroll example
Defining the variables:
sName
Fred Bloggs
John Smith
Jim Brown
Dave Boss
cSalary
0
1
2
3
4
5
6
7
8
9
10,200
14,000
8,250
25,600
Private sName (10) As String
Private cSalary (10) As Currency
Private ID As Integer
Private cTotalPayroll As Currency
Program code
Private
Private
Private
Private
sName (10) As String
cSalary (10) As Currency
ID As Integer
cTotalPayroll As Currency
Private Sub cmdEmployee_Click()
ID = scrEmployee.Value
‘Set array index
‘depending on scroll bar
sName(ID) = txtName.Text
cSalary(ID) = txtSalary.Text
‘Enter data from text
‘boxes into arrays
cTotalPayroll = cTotalPayroll + cSalary(ID)
‘Add to total pay
scrEmployee.Value = scrEmployee.Value + 1
‘Move scroll bar
‘on 1 position
End Sub
Private
Private
Private
Private
Program code
sName (10) As String
cSalary (10) As Currency
ID As Integer
cTotalPayroll As Currency
When the scroll bar is moved :
Private Sub scrEmployee_Change()
ID = scrEmployee.Value
‘Set array index depending
‘on scroll bar
‘Display name & salary from
‘appropriate position of arrays
txtName.Text = sNames(ID)
txtSalary.Text = Format(cSalary(ID), "Currency")
End Sub
Arrays can have more than 1 dimension
Occasionally information can often be presented
more clearly by using arrays with more than one
dimension.
Row
1
2
3
4
5
1
8
8
8
8
8
2
45
45
45
45
45
3
23
23
23
23
23
4
100
Col
41
18
15
13
Arrays can have more than 1 dimension
E.g
Dim iAllMarks (1 To 5, 1 To 4) As Integer
columns
Row
rows
1
2
3
4
5
1
8
8
8
8
8
2
45
45
45
45
45
3
23
23
23
23
23
4
100
Col
41
18
iAllMarks (2, 4) = 41
15
13
Accessing 2-D arrays
Usually done using two nested loops
For col 1 to 5
For row 1 to 3
Store input in cell (col,row)
Next row
Next col
For row 1 to 3
For col 1 to 5
Store input in cell (col,row)
Next col
Next row
Single line or column?
By keeping the column number the same and varying row –
access a single column
e.g.txtOutput.Text = marks(3,row) [in loop]
By keeping the row number the same and varying col – access
a single row
e.g.txtOutput.Text = marks(col,1) [in loop]
Array example - sorting
In this example a
simple set of inputs
are set up.
Clicking the top button
allows data entered to
be stored in the array
The middle one sorts
the data
The bottom button
puts sorted data in the
text boxes
Set Global variables and Initialise data
Const cmin = 0
Const cmax = 4
‘declare data array’
Private iNumbers(cmin To cmax) As Integer
Sub Form_Load ()
Dim i As Integer
For i = cmin To cmax
iNumbers(i) = 0
Next i
text1 = iNumbers(0)
text2 = iNumbers(1)
text3 = iNumbers(2)
text4 = iNumbers(3)
text5 = iNumbers(4)
End Sub
‘initialise array elements to zero
‘initialise text boxes
Store Numbers
Sub cmdAssign_Click ()
If (text1.Text = "") Or (text2.Text = "") Or (text3.Text = "") Or
(text4.Text = "") Or (text5.Text = "") Then
Beep
MsgBox ("a zero length string is present")
Else
‘store data from textboxes into array
iNumbers(0) = CInt(text1.Text)
iNumbers(1) = CInt(text2.Text)
iNumbers(2) = CInt(text3.Text)
iNumbers(3) = CInt(text4.Text)
iNumbers(4) = CInt(text5.Text)
End If
End Sub
Sort Numbers
Sub cmdRearrange_Click ()
Dim i As Integer
Dim iPass As Integer
Dim iTemp As Integer
Dim iNoSwitches As Integer
iPass = 0
Do
iPass = iPass + 1
iNoSwitches = 1
For i = cmin To (cmax - iPass)
If iNumbers(i) > iNumbers(i + 1) Then
iNoSwitches = 0
iTemp = iNumbers(i)
iNumbers(i) = iNumbers(i + 1)
iNumbers(i + 1) = iTemp
End If
Next i
Loop Until NoSwitches = 1
End Sub
Redisplay numbers
Sub cmdRetrieve_Click ()
label1.Caption = iNumbers(0)
label2.Caption = iNumbers(1)
label3.Caption = iNumbers(2)
label4.Caption = iNumbers(3)
label5.Caption = iNumbers(4)
End Sub
Summing up
Simple data arrays can be thought of as tables of
data
Arrays enable us to reference several data items
using one variable name
They can be 2-dimensional (or 3, or 4 …)
They are almost always processed using loops