View
213
Download
0
Tags:
Embed Size (px)
Citation preview
Arrays in Visual Basic
Week 9CM30104-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 VBData 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
1
2
3
4
5
iNos (3)
Array name
Array index
Array elementsCoding to reference each element
42
31
98
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
1 42
2 98
3 21
4 77
5 30
0 42
1 98
2 21
3 77
4 30When no start number specifiedarray index begins at 0
An example - Finding largest of a sequence of numbers
Using several variables … iNum1, iNum2, iNum3 etcand a complicated Nested If ………….
If (iNum1 > iNum2) And (iNum1 > iNum2) And … … And (iNum1 > iNum5) Then
iLargest = iNum1Else
If (iNum2 > iNum1) And (iNum2 > iNum3) …… …….. etc
An example - Finding largest of a sequence of numbers
Using a For loop and one variable, iNum
For iLoopCount = 1 To 5
iNum = InputBox (“Enter next number”)
If iNum > iLargest TheniLargest = iNum
End If
Next iLoopCount
Give iLargest an initialvery small value
iLargest = 0
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 TheniLargest = iNos (iCount)
End If
Next iCount
iNos
45132
2
71
94
iLargest
132
Finding largest of a sequence of marks & also storing names associated with each mark
iMarks(1 To 5)
42
98
21
77
30
sNames(1 To 5)
Fred
Sue
Bill
Amy
Jonathon
iHighestMark
98
sBestStudent
Sue
Finding largest of a sequence of marks & also storing names associated with each mark
iMarks(1 To 5) sNames(1 To 5)
42
98
21
77
30
Fred
Sue
Bill
Amy
Jonathon
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
98
77
42
30
21
Sue
Amy
Fred
Jonathon
Bill
42
98
21
77
30
Fred
Sue
Bill
Amy
Jonathon
ORDER OF INPUT ARRAYS AFTER SORTING
Example coding
‘fill arraysFor iCount = 1 to 5
sNames(iCount) = InputBox(“Name”)iMarks(iCount) = InputBox(“Mark”)
Next iCount
‘display to listboxesFor iCount = 1 to 5
lstNames.AddItem sNames(iCount)lstMarks.AddItem iMarks(iCount)
Next iCount
42
98
21
77
30
Fred
Sue
Bill
Amy
Jonathon
Payroll example
Min = 0; Max = 9 Vertical Scroll Bar
scrEmployee
Caption = “Add Employee”
Command Button
cmdEmployee
Caption = “”LabellblPayroll
Text = “”Text BoxtxtSalary
Text = “”Text BoxtxtName
PropertiesControlName
• Using a form like this to:
• Input name & salary• Store data in an array
• Scroll through previous data entered
Payroll example
• Scroll bar set at design stage to
• min = 0
• max = 9
sName cSalary
Fred Bloggs 0 10,200John Smith 1 14,000Jim Brown 2 8,250Dave Boss 3 25,600
456789
• Data stored in 2 arrays:
sName (10) As String sSalary (10) As Currency
Payroll example
Defining the variables:
Private sName (10) As StringPrivate cSalary (10) As Currency
Private ID As Integer
Private cTotalPayroll As Currency
sName cSalary
Fred Bloggs 0 10,200John Smith 1 14,000Jim Brown 2 8,250Dave Boss 3 25,600
456789
Program code
Private Sub cmdEmployee_Click()
ID = scrEmployee.Value ‘Set array index ‘depending on scroll bar
sName(ID) = txtName.Text ‘Enter data from text cSalary(ID) = txtSalary.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 sName (10) As StringPrivate cSalary (10) As CurrencyPrivate ID As IntegerPrivate cTotalPayroll As Currency
Program code Private sName (10) As StringPrivate cSalary (10) As CurrencyPrivate ID As IntegerPrivate 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.
23232323233
45454545452
888881
Col 1 2 3 4 5
4 100 41
18 15 13
Row
Arrays can have more than 1 dimension
E.g Dim iAllMarks (1 To 5, 1 To 4) As Integer
columns rows
23232323233
45454545452
888881
Col 1 2 3 4 5
4 100 41 18 15 13
Row
iAllMarks (2, 4) = 41
Accessing 2-D arrays
Usually done using two nested loops
For col 1 to 5For row 1 to 3Store input in cell (col,row)Next row
Next col
For row 1 to 3For col 1 to 5Store 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 = 0Const cmax = 4 ‘declare data array’ Private iNumbers(cmin To cmax) As Integer
Sub Form_Load ()Dim i As Integer ‘initialise array elements to zero For i = cmin To cmax iNumbers(i) = 0 Next i ‘initialise text boxes text1 = iNumbers(0) text2 = iNumbers(1) text3 = iNumbers(2) text4 = iNumbers(3) text5 = iNumbers(4)End Sub
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 IntegerDim iPass As IntegerDim iTemp As IntegerDim iNoSwitches As Integer
iPass = 0Do 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 iLoop 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