Download presentation
Presentation is loading. Please wait.
1
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules
2
Mark Dixon, SoCCE SOFT 131Page 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 user A&E PAS
3
Mark Dixon, SoCCE SOFT 131Page 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
4
Mark Dixon, SoCCE SOFT 131Page 4 Example 1: Employee Data Need to keep a record of employee details –e.g. surname forenames date of birth address telephone number salary
5
Mark Dixon, SoCCE SOFT 131Page 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
6
Mark Dixon, SoCCE SOFT 131Page 6 Example 1: Code Design 2 layers: Layer 1 Event Handler Procedures Layer 2 General Procedures btnPrevious Click btnNext Click Form Load Employee Display Employee Store
7
Mark Dixon, SoCCE SOFT 131Page 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
8
Mark Dixon, SoCCE SOFT 131Page 8 Example 1: Employees v1 Option Explicit Dim Surnames(1 To 10) As String Dim Forenames(1 To 10) As String Dim Salaries(1 To 10) As Double Dim 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 EmpDisplay End Sub Private Sub btnNext_Click() EmpStore curEmp = curEmp + 1 EmpDisplay End Sub Employees v1
9
Mark Dixon, SoCCE SOFT 131Page 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
10
Mark Dixon, SoCCE SOFT 131Page 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"
11
Mark Dixon, SoCCE SOFT 131Page 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"
12
Mark Dixon, SoCCE SOFT 131Page 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 Double End Type Dim myHouse As THouse myHouse.Street = "Portland Square"
13
Mark Dixon, SoCCE SOFT 131Page 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 String End Type Dim myCar As TCar myCar.RegNum = "GH23 XRB"
14
Mark Dixon, SoCCE SOFT 131Page 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: double Forenames: string each row is a TEmployee makes it easier to get details of single employee
15
Mark Dixon, SoCCE SOFT 131Page 15 Example 2: Employees v2 Option Explicit Private Type TEmployee Surname As String Forenames As String Salary As Double End Type Dim Employees(1 To 10) As TEmployee Dim curEmp As Integer Sub EmpDisplay() lblEmpNum.Caption = curEmp txtSurname.Text = Employees(curEmp).Surname txtForenames.Text = Employees(curEmp).Forenames txtSalary.Text = Employees(curEmp).Salary End 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 EmpDisplay End Sub Private Sub btnNext_Click() EmpStore curEmp = curEmp + 1 EmpDisplay End Sub Employees v2
16
Mark Dixon, SoCCE SOFT 131Page 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
17
Mark Dixon, SoCCE SOFT 131Page 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
18
Mark Dixon, SoCCE SOFT 131Page 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
19
Mark Dixon, SoCCE SOFT 131Page 19 Example 3: Employees v3 Employees v3 Option Explicit Private Sub Form_Load() curEmp = 1 EmpDisplay End Sub Private Sub btnNext_Click() EmpStore curEmp = curEmp + 1 EmpDisplay End Sub Option Explicit Private Type TEmployee Surname As String Forenames As String Salary As Double End Type Dim Employees(1 To 10) As TEmployee Public 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).Salary End Sub Sub EmpStore() Employees(curEmp).Surname = frmMain.txtSurname.Text Employees(curEmp).Forenames = frmMain.txtForenames.Text Employees(curEmp).Salary = Val(frmMain.txtSalary.Text) End Sub frmMainmodEmployees
20
Mark Dixon, SoCCE SOFT 131Page 20 Example 4: Multiple Forms Show method – displays form Hide method – hides form
21
Mark Dixon, SoCCE SOFT 131Page 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 AProject B Form 1Form 2 Module 2 Form 4Form 3 Module 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.