1 / 21

11 – Arrays of Structures & Modules

11 – Arrays of Structures & Modules. Assignment. Individual Assignment do not copy other people's assignment ( plagiarism ) do help each other understand lectures and tutorials ( peer support )

adara
Télécharger la présentation

11 – Arrays of Structures & Modules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 11 – Arrays of Structures & Modules

  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. 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. Example 1: Employee Data • Need to keep a record of employee details • e.g. • surname • forenames • date of birth • address • telephone number • salary

  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. Example 1: Code Design • 2 layers: Layer 1 Event HandlerProcedures Layer 2 GeneralProcedures FormLoad EmployeeDisplay btnNextClick EmployeeStore btnPreviousClick

  7. Example 1: Data Design Surnames: string Forenames: string Salaries: double 1 1 1 5 5 5 10 10 10 • 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

  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. 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. 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. 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. 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"

  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"

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

  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. 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. 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. 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. Example 3: Employees v3 frmMain modEmployees 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 Employees v3

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

  21. Modules: Sharing Project A Project B Module 1 Module 2 Form 1 Form 2 Form 3 Form 4 • Can share modules between projects: • Click the File menu • Click the Add File menu item • Select the module file • Press the [Return] key

More Related