11 Arrays of Structures Modules Mark Dixon So

  • Slides: 21
Download presentation
11 – Arrays of Structures & Modules Mark Dixon, So. CCE SOFT 131 Page

11 – Arrays of Structures & Modules Mark Dixon, So. CCE SOFT 131 Page 1

Assignment • Individual Assignment – do not copy other people's assignment (plagiarism) – do

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 Mark Dixon, So. CCE SOFT 131 Page 2

Session Aims & Objectives • Aims – To introduce the idea of an array

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, So. CCE SOFT 131 Page 3

Example 1: Employee Data • Need to keep a record of employee details –

Example 1: Employee Data • Need to keep a record of employee details – e. g. • • • Mark Dixon, So. CCE surname forenames date of birth address telephone number salary SOFT 131 Page 4

Example 1: User Interface • Must respond to following events: • Click Previous button:

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, So. CCE SOFT 131 Page 5

Example 1: Code Design • 2 layers: Layer 1 Event Handler Procedures Layer 2

Example 1: Code Design • 2 layers: Layer 1 Event Handler Procedures Layer 2 General Procedures Form Load btn. Next Click btn. Previous Click Mark Dixon, So. CCE SOFT 131 Employee Display Employee Store Page 6

Example 1: Data Design • We could use an array for each piece of

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 Forenames: string Salaries: double 1 1 1 5 5 5 10 10 10 Mark Dixon, So. CCE SOFT 131 Page 7

Example 1: Employees v 1 Option Explicit Dim Surnames(1 To 10) As String Dim

Example 1: Employees v 1 Option Explicit Dim Surnames(1 To 10) As String Dim Forenames(1 To 10) As String Dim Salaries(1 To 10) As Double Dim cur. Emp As Integer Sub Emp. Display() lbl. Emp. Num. Caption = cur. Emp txt. Surname. Text = Surnames(cur. Emp) txt. Forenames. Text = Forenames(cur. Emp) txt. Salary. Text = Salaries(cur. Emp) End Sub Emp. Store() Surnames(cur. Emp) = txt. Surname. Text Forenames(cur. Emp) = txt. Forenames. Text Salaries(cur. Emp) = Val(txt. Salary. Text) End Sub Private Sub Form_Load() cur. Emp = 1 Emp. Display End Sub Private Sub btn. Next_Click() Emp. Store cur. Emp = cur. Emp + 1 Emp. Display End Sub Employees v 1 Mark Dixon, So. CCE SOFT 131 Page 8

Difficulty • This design works • However, if – all fields were implemented, and

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, So. CCE SOFT 131 Page 9

Structures • Groups different types of data • Declaration of type: Type TAnimal Name

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 my. Pet As TAnimal • Change value of My. Pet’s name: my. Pet. Name = "George" Mark Dixon, So. CCE SOFT 131 Page 10

Array of Structures • Can also have arrays of structures: Dim My. Pets(1 To

Array of Structures • Can also have arrays of structures: Dim My. Pets(1 To 5) As TAnimal. Rec • Change value: My. Pets(3). Name = "George" • Change value using index variable: ind = 2 My. Pets(ind). Name = "Fred" Mark Dixon, So. CCE SOFT 131 Page 11

Exercise 1: Structures • Create a record definition for: – Estate agents: House details

Exercise 1: Structures • Create a record definition for: – Estate agents: House details (house num. , street, price) Type THouse Num As Long Street As String Price As Double End Type • Write code that will: – Create a variable of the above type Dim my. House As THouse – Put data into the elements of that variable my. House. Street = "Portland Square" Mark Dixon, So. CCE SOFT 131 Page 12

Exercise 2: Structures • Create a record definition for: – Police stolen car register:

Exercise 2: Structures • Create a record definition for: – Police stolen car register: Car details (Reg. number, colour, model) Type TCar Reg. Num As String Colour As String Model As String End Type • Write code that will: – Create a variable of the above type Dim my. Car As TCar – Put data into the elements of that variable my. Car. Reg. Num = "GH 23 XRB" Mark Dixon, So. CCE SOFT 131 Page 13

Example 2: Data Design • We can now use a single array that uses

Example 2: Data Design • We can now use a single array that uses a user defined type/record/structure: Employees: TEmployee 1 each row is a TEmployee 5 10 Surname: string Forenames: string Salary: double • makes it easier to get details of single employee Mark Dixon, So. CCE SOFT 131 Page 14

Example 2: Employees v 2 Option Explicit Private Type TEmployee Surname As String Forenames

Example 2: Employees v 2 Option Explicit Private Type TEmployee Surname As String Forenames As String Salary As Double End Type Dim Employees(1 To 10) As TEmployee Dim cur. Emp As Integer Sub Emp. Display() lbl. Emp. Num. Caption = cur. Emp txt. Surname. Text = Employees(cur. Emp). Surname txt. Forenames. Text = Employees(cur. Emp). Forenames txt. Salary. Text = Employees(cur. Emp). Salary End Sub Emp. Store() Employees(cur. Emp). Surname = txt. Surname. Text Employees(cur. Emp). Forenames = txt. Forenames. Text Employees(cur. Emp). Salary = Val(txt. Salary. Text) End Sub Private Sub Form_Load() cur. Emp = 1 Emp. Display End Sub Employees v 2 Mark Dixon, So. CCE Private Sub btn. Next_Click() Emp. Store cur. Emp = cur. Emp + 1 Emp. Display End Sub SOFT 131 Page 15

Multiple Modules Forms • Projects can contain many modules/units – form modules (*. FRM)

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, So. CCE SOFT 131 Page 16

Multiple Forms: Start Form • To set the start form: – – – Click

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, So. CCE SOFT 131 Page 17

Public & Private • Private – can only be used in current module •

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, So. CCE SOFT 131 Page 18

Example 3: Employees v 3 frm. Main Option Explicit mod. Employees Option Explicit Private

Example 3: Employees v 3 frm. Main Option Explicit mod. Employees Option Explicit Private Sub Form_Load()Private Type TEmployee Surname As String cur. Emp = 1 Forenames As String Emp. Display Salary As Double End Sub End Type Private Sub btn. Next_Click() Emp. Store Dim Employees(1 To 10) As TEmployee cur. Emp = cur. Emp + 1 Public cur. Emp As Integer Emp. Display Sub Emp. Display() End Sub frm. Main. lbl. Emp. Num. Caption = cur. Emp frm. Main. txt. Surname. Text = Employees(cur. Emp). Surn frm. Main. txt. Forenames. Text = Employees(cur. Emp). Fo frm. Main. txt. Salary. Text = Employees(cur. Emp). Salary End Sub Employees v 3 Mark Dixon, So. CCE Sub Emp. Store() Employees(cur. Emp). Surname = frm. Main. txt. Surname Employees(cur. Emp). Forenames = frm. Main. txt. Forena Employees(cur. Emp). Salary = Val(frm. Main. txt. Salary. T End Sub SOFT 131 Page 19

Example 4: Multiple Forms • Show method – displays form • Hide method –

Example 4: Multiple Forms • Show method – displays form • Hide method – hides form Mark Dixon, So. CCE SOFT 131 Page 20

Modules: Sharing • Can share modules between projects: – Click the File menu –

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 Module 1 Mark Dixon, So. CCE Form 1 Form 2 Project B Module 2 SOFT 131 Form 3 Form 4 Page 21