Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
-
date post
22-Dec-2015 -
Category
Documents
-
view
220 -
download
0
Transcript of Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Mark Dixon, SoCCE SOFT 131 Page 1
11 – Arrays of Structures & Modules
Mark Dixon, SoCCE SOFT 131 Page 2
Assignment• Individual Assignment
– do not copy other people's assignment (plagiarism)
– do help each other understand lectures and tutorials (peer support)
• Backups – floppy disks – 10% failure rate– last corrupt disk 2 weeks ago
• Codes – don't show userA&E
PAS
Mark Dixon, SoCCE SOFT 131 Page 3
Session Aims & Objectives• Aims
– To introduce the idea of an array of structures– To introduce the idea of modules
• Objectives,by end of this week’s sessions, you should be able to:
– create and use an array of structures– appropriately split a program into multiple
modules
Mark Dixon, SoCCE SOFT 131 Page 4
Example 1: Employee Data• Need to keep a record of employee details
– e.g.• surname• forenames• date of birth• address• telephone number• salary
Mark Dixon, SoCCE SOFT 131 Page 5
Example 1: User Interface• Must respond to following events:
• Click Previous button: move to previous employee’s details
• Click Next button: move to next employee’s details• Type in fields: change current employee’s details
Mark Dixon, SoCCE SOFT 131 Page 6
Example 1: Code Design• 2 layers:
Layer 1Event Handler
Procedures
Layer 2General
Procedures
btnPreviousClick
btnNextClick
FormLoad
EmployeeDisplay
EmployeeStore
Mark Dixon, SoCCE SOFT 131 Page 7
Example 1: Data Design•We could use an array for each piece of employee information: Dim Surnames(1 To 10) As String Dim Forenames(1 To 10) As String Dim Salaries(1 To 10) As Double
Surnames: string
5
10
1
Forenames: string
5
10
1
Salaries: double
5
10
1
Mark Dixon, SoCCE SOFT 131 Page 8
Example 1: Employees v1Option ExplicitDim Surnames(1 To 10) As StringDim Forenames(1 To 10) As StringDim Salaries(1 To 10) As DoubleDim curEmp As Integer
Sub EmpDisplay() lblEmpNum.Caption = curEmp txtSurname.Text = Surnames(curEmp) txtForenames.Text = Forenames(curEmp) txtSalary.Text = Salaries(curEmp)End Sub
Sub EmpStore() Surnames(curEmp) = txtSurname.Text Forenames(curEmp) = txtForenames.Text Salaries(curEmp) = Val(txtSalary.Text)End Sub
Private Sub Form_Load() curEmp = 1 EmpDisplayEnd Sub
Private Sub btnNext_Click() EmpStore curEmp = curEmp + 1 EmpDisplayEnd Sub
Employees v1
Mark Dixon, SoCCE SOFT 131 Page 9
Difficulty• This design works• However, if
– all fields were implemented, and– more complex operations were added
• the code would become difficult to manage– having several separate arrays
• Arrays allow data to be grouped– however, arrays must be homogenous (same data
type)
• it would be useful to be able to group different (heterogeneous) types of data
Mark Dixon, SoCCE SOFT 131 Page 10
Structures• Groups different types of data
• Declaration of type: Type TAnimal Name As String Species As String Gender As Boolean End Type
• Use of type (in variable declaration):
Dim myPet As TAnimal• Change value of MyPet’s name:
myPet.Name = "George"
Mark Dixon, SoCCE SOFT 131 Page 11
Array of Structures
• Can also have arrays of structures:
Dim MyPets(1 To 5) As TAnimalRec• Change value:
MyPets(3).Name = "George"• Change value using index variable:
ind = 2
MyPets(ind).Name = "Fred"
Mark Dixon, SoCCE SOFT 131 Page 12
Exercise 1: Structures• Create a record definition for:
– Estate agents:House details (house num., street, price)
• Write code that will:– Create a variable of the above type
– Put data into the elements of that variable
Type THouse Num As Long Street As String Price As DoubleEnd Type
Dim myHouse As THouse
myHouse.Street = "Portland Square"
Mark Dixon, SoCCE SOFT 131 Page 13
Exercise 2: Structures• Create a record definition for:
– Police stolen car register:Car details (Reg. number, colour, model)
• Write code that will:– Create a variable of the above type
– Put data into the elements of that variable
Type TCar RegNum As String Colour As String Model As StringEnd Type
Dim myCar As TCar
myCar.RegNum = "GH23 XRB"
Mark Dixon, SoCCE SOFT 131 Page 14
Example 2: Data Design• We can now use a single array that uses a
user defined type/record/structure:
Surname: string
Employees: TEmployee
5
10
1
Salary: doubleForenames: string
each row isa TEmployee
• makes it easier to get details of single employee
Mark Dixon, SoCCE SOFT 131 Page 15
Example 2: Employees v2Option Explicit
Private Type TEmployee Surname As String Forenames As String Salary As DoubleEnd Type
Dim Employees(1 To 10) As TEmployeeDim curEmp As Integer
Sub EmpDisplay() lblEmpNum.Caption = curEmp txtSurname.Text = Employees(curEmp).Surname txtForenames.Text = Employees(curEmp).Forenames txtSalary.Text = Employees(curEmp).SalaryEnd Sub
Sub EmpStore()
Employees(curEmp).Surname = txtSurname.Text Employees(curEmp).Forenames = txtForenames.Text Employees(curEmp).Salary = Val(txtSalary.Text)End Sub
Private Sub Form_Load() curEmp = 1 EmpDisplayEnd Sub
Private Sub btnNext_Click() EmpStore curEmp = curEmp + 1 EmpDisplayEnd Sub
Employees v2
Mark Dixon, SoCCE SOFT 131 Page 16
Multiple Modules Forms• Projects can contain many modules/units
– form modules (*.FRM)• Click the Project menu• Click the Add Form menu item
– code modules (*.BAS)• Click the Project menu• Click the Add Module menu item
• Modules– divide your code into separate parts– available to other forms and code modules
Mark Dixon, SoCCE SOFT 131 Page 17
Multiple Forms: Start Form• To set the start form:
– Click the Project menu– Click the Properties menu item– Click the General tab– Click the Start up object list– Select a form
Mark Dixon, SoCCE SOFT 131 Page 18
Public & Private• Private – can only be used in current
module
• Public – can be used by any module
• Used for:– module level variables (instead of dim) Private x As Integer
– procedures and functions (start of declaration) Private Sub Display() … End Sub
Mark Dixon, SoCCE SOFT 131 Page 19
Example 3: Employees v3
Employees v3
Option Explicit
Private Sub Form_Load() curEmp = 1 EmpDisplayEnd Sub
Private Sub btnNext_Click() EmpStore curEmp = curEmp + 1 EmpDisplayEnd Sub
Option Explicit
Private Type TEmployee Surname As String Forenames As String Salary As DoubleEnd Type
Dim Employees(1 To 10) As TEmployeePublic curEmp As Integer
Sub EmpDisplay() frmMain.lblEmpNum.Caption = curEmp frmMain.txtSurname.Text = Employees(curEmp).Surname frmMain.txtForenames.Text = Employees(curEmp).Forenames frmMain.txtSalary.Text = Employees(curEmp).SalaryEnd Sub
Sub EmpStore() Employees(curEmp).Surname = frmMain.txtSurname.Text Employees(curEmp).Forenames = frmMain.txtForenames.Text Employees(curEmp).Salary = Val(frmMain.txtSalary.Text)End Sub
frmMain modEmployees
Mark Dixon, SoCCE SOFT 131 Page 20
Example 4: Multiple Forms
Option Explicit
Sub cmdSecond_Click () frmSecond.Show 1End Sub
Option ExplicitDim num As Integer
Sub cmdAdd_Click () num = num + 5End Sub
Sub cmdHide_Click () frmSecond.HideEnd Sub
Sub cmdNum_Click () picNum.Cls picNum.Print numEnd Sub
Sub cmdUnload_Click () Unload frmSecondEnd Sub
Sub Form_Load () txtSecond = "" num = 12End Sub
• Show method – displays form
• Hide method – hides form
Mark Dixon, SoCCE SOFT 131 Page 21
Modules: Sharing• Can share modules between projects:
– Click the File menu– Click the Add File menu item– Select the module file– Press the [Return] key
Project A Project B
Form 1 Form 2 Module 2 Form 4Form 3Module 1