1 / 25

18 – Enumerated Data Types and Arrays of Structures

18 – Enumerated Data Types and Arrays of Structures. Session Aims & Objectives. Aims, to introduce: the idea of enumerated data types the idea of an array of structures Objectives, by end of this week’s sessions, you should be able to: declare and use an enumerated data type

leda
Télécharger la présentation

18 – Enumerated Data Types and Arrays of Structures

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. 18 – Enumerated Data Types andArrays of Structures

  2. Session Aims & Objectives • Aims, to introduce: • the idea of enumerated data types • the idea of an array of structures • Objectives,by end of this week’s sessions, you should be able to: • declare and use an enumerated data type • create and use an array of structures

  3. Enumerated Data Types • Often need to use numbers to represent things (coding) • For example, curry: mild, medium, or hot • Could store text: "mild", "medium", "hot" • takes lots of space (1 byte per character) • easily becomes inconsistent, e.g. "hit“ vs. “hot” • Alternatively, use numbers to represent text: 1 "mild" 2 "medium" 3 "hot"

  4. Example: Curry v1 Option Explicit Private Sub Form_Load() lstCurry.AddItem "Mild", 0 lstCurry.AddItem "Medium", 1 lstCurry.AddItem "Hot", 2 picCurry.FillStyle = vbSolid End Sub Private Sub lstCurry_Click() lblCurryCode.Caption = lstCurry.ListIndex lblCurryText.Caption = lstCurry.List(lstCurry.ListIndex) If lstCurry.ListIndex = 0 Then picCurry.FillColor = vbWhite ElseIf lstCurry.ListIndex = 1 Then picCurry.FillColor = vbYellow Else picCurry.FillColor = vbRed End If picCurry.Cls picCurry.Circle (1000, 750), 500 End Sub Curry v1

  5. Example: Curry v2 Option Explicit Private Sub Form_Load() lstCurry.AddItem "Mild", 0 lstCurry.AddItem "Medium", 1 lstCurry.AddItem "Hot", 2 picCurry.FillStyle = vbSolid End Sub Private Sub lstCurry_Click() Dim CuCo As Long ' Curry code CuCo = lstCurry.ListIndex lblCurryCode.Caption = CuCo lblCurryText.Caption = lstCurry.List(CuCo) If CuCo = 0 Then picCurry.FillColor = vbWhite ElseIf CuCo = 1 Then picCurry.FillColor = vbYellow Else picCurry.FillColor = vbRed End If picCurry.Cls picCurry.Circle (1000, 750), 500 End Sub Curry v2

  6. Example: Curry v3 Option Explicit Const Mild = 0 Const Medium = 1 Const Hot = 2 Private Sub Form_Load() lstCurry.AddItem "Mild", Mild lstCurry.AddItem "Medium", Medium lstCurry.AddItem "Hot", Hot picCurry.FillStyle = vbSolid End Sub Private Sub lstCurry_Click() Dim CuCo As Long ' Curry code CuCo = lstCurry.ListIndex lblCurryCode.Caption = CuCo lblCurryText.Caption = lstCurry.List(CuCo) If CuCo = Mild Then picCurry.FillColor = vbWhite ElseIf CuCo = Medium Then picCurry.FillColor = vbYellow Else picCurry.FillColor = vbRed End If picCurry.Cls picCurry.Circle (1000, 750), 500 End Sub Curry v3

  7. Example: Curry v4 Option Explicit Enum TSpice Mild = 0 Medium = 1 Hot = 2 End Enum Private Sub Form_Load() lstCurry.AddItem "Mild", Mild lstCurry.AddItem "Medium", Medium lstCurry.AddItem "Hot", Hot picCurry.FillStyle = vbSolid End Sub Private Sub lstCurry_Click() Dim CuCo As TSpice ' Curry code CuCo = lstCurry.ListIndex lblCurryCode.Caption = CuCo lblCurryText.Caption = lstCurry.List(CuCo) If CuCo = Mild Then picCurry.FillColor = vbWhite ElseIf CuCo = Medium Then picCurry.FillColor = vbYellow Else picCurry.FillColor = vbRed End If picCurry.Cls picCurry.Circle (1000, 750), 500 End Sub Curry v4

  8. Questions: EDTs • Create an EDT to store the following classification of height: short, average, tall • Create an EDT to store the following classification of publication: book, journal Enum THeight Short = 0 Average = 1 Tall = 2 End Enum Enum TPublication Book = 0 Journal = 1 End Enum

  9. Example: Employee Data • Need to keep a record of employee details • e.g. • surname • forenames • date of birth • address • telephone number • salary

  10. Example: 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

  11. Example: Code Design • 2 layers: Layer 1 Event HandlerProcedures Layer 2 GeneralProcedures FormLoad EmployeeDisplay btnNextClick EmployeeStore btnPreviousClick

  12. Example: 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

  13. Example: 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

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

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

  16. Structures: Pets

  17. Array of Structures • Can also have arrays of structures: Dim MyPets(1 To 5) As TAnimal • Change value: MyPets(3).Name = "George" • Change value using index variable: ind = 2 MyPets(ind).Name = "Fred"

  18. Array of Structures: Pets

  19. Questions: 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"

  20. Questions: 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"

  21. Example: 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

  22. Example: 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

  23. Tutorial Exercises: Curry • LEARNING OBJECTIVE:to understand enumerated data types • Task 1: Get the Curry examples from the lecture working. • Task 2: Modify your code – add an extra category called ‘Blazing’ with a code value of 3.

  24. Tutorial Exercises: Employees • LEARNING OBJECTIVE:to understand arrays of structures • Task 1: Get the Employees examples from the lecture working. • Task 2: Modify your code – add code that allows the user to go to a previous record. • Task 3: Modify your code – to prevent the user going too far forward or back (i.e. so they can’t go back from the first record and can’t go forward from the last record).

  25. Tutorial Exercises: Pets • LEARNING OBJECTIVE:use enumerated data types with an array of structures • Task 1: Create a new project that keeps a record of 10 sets of Pet Details in a Veterinary Surgery. You should store: • Owner Surname • Owner Forenames • Pet Name • Pet Species • Pet Gender • Task 2: Use enumerated data types to store the species and gender data.

More Related