Download presentation
Presentation is loading. Please wait.
1
Arrays in Visual Basic Week 9 CM30104-1
2
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
3
Types of Arrays There are 2 types of arrays in VB : Data Arrays Control Arrays
4
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.
5
Data arrays ~ example iNos 1 2 3 4 5 iNos (3) Array name Array index Array elements Coding to reference each element 42 31 98
6
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
7
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
8
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 Then iLargest = iNum End If Next iLoopCount Give iLargest an initial very small value iLargest = 0
9
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 45 132 2 71 94 iLargest 132
10
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
11
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
12
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 INPUTARRAYS AFTER SORTING
13
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 98 21 77 30 Fred Sue Bill Amy Jonathon
14
Payroll example Min = 0; Max = 9Vertical Scroll Bar scrEmployee Caption = “Add Employee” Command Button cmdEmployee Caption = “”Label lblPayroll Text = “”Text Box txtSalary Text = “”Text Box txtName PropertiesControl Name Using a form like this to: Input name & salary Store data in an array Scroll through previous data entered
15
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
16
Payroll example Defining the variables: Private sName (10) As String Private cSalary (10) As Currency Private ID As Integer Private cTotalPayroll As Currency
17
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 String Private cSalary (10) As Currency Private ID As Integer Private cTotalPayroll As Currency
18
Program code Private sName (10) As String Private cSalary (10) As Currency Private ID As Integer Private 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
19
Arrays can have more than 1 dimension Occasionally information can often be presented more clearly by using arrays with more than one dimension. 23 3 45 2 88888 1 Col 1 2345 4 10041181513 Row
20
Arrays can have more than 1 dimension E.g Dim iAllMarks (1 To 5, 1 To 4) As Integer columns rows iAllMarks (2, 4) = 41
21
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
22
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]
23
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
24
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 ‘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
25
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
26
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
27
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
28
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.