280 likes | 432 Views
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.
E N D
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.
Array name 1 2 iNos (3) 3 4 5 Array index Coding to reference each element Array elements Data arrays ~ example iNos 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 When no start number specified array index begins at 0
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
Give iLargest an initial very small value 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 iLargest = 0
iNos iLargest 132 45 132 2 71 94 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
iHighestMark 42 Fred 98 98 Sue 21 Bill Sue 77 Amy sBestStudent 30 Jonathon iMarks(1 To 5) sNames(1 To 5) Finding largest of a sequence of marks & also storing names associated with each mark
42 Fred 98 Sue 21 Bill 77 Amy 30 Jonathon iMarks(1 To 5) sNames(1 To 5) Finding largest of a sequence of marks & also storing names associated with each mark 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
ORDER OF INPUT 42 Fred 98 Sue 98 Sue 77 Amy 21 Bill 42 Fred 77 Amy 30 Jonathon 30 Jonathon 21 Bill Arrays have many advantages –e.g, we can then sort & list students in order ARRAYS AFTER SORTING
42 Fred 98 Sue 21 Bill 77 Amy 30 Jonathon 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
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 Payroll example • 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 • Data stored in 2 • arrays: • sName (10) As String • sSalary (10) As Currency
Payroll example Defining the variables: Private sName (10) As String Private cSalary (10) As Currency Private ID As Integer Private cTotalPayroll As Currency
Program code Private sName (10) As String Private cSalary (10) As Currency Private ID As Integer Private cTotalPayroll As Currency 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
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
1 8 8 8 8 8 2 45 45 45 45 45 3 23 23 23 23 23 Arrays can have more than 1 dimension • Occasionally information can often be presented more clearly by using arrays with more than one dimension. Row 2 3 4 5 1 Col 100 41 18 15 13 4
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
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 ‘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 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