Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures

2 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 9 Example: Employee Data Need to keep a record of employee details –e.g. surname forenames date of birth address telephone number salary

10 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 11 Example: Code Design 2 layers: Layer 1 Event Handler Procedures Layer 2 General Procedures btnPrevious Click btnNext Click Form Load Employee Display Employee Store

12 Mark Dixon, SoCCE SOFT 131Page 12 Example: 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

13 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 16 Structures: Pets

17 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 18 Array of Structures: Pets

19 Mark Dixon, SoCCE SOFT 131Page 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 Double End Type Dim myHouse As THouse myHouse.Street = "Portland Square"

20 Mark Dixon, SoCCE SOFT 131Page 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 String End Type Dim myCar As TCar myCar.RegNum = "GH23 XRB"

21 Mark Dixon, SoCCE SOFT 131Page 21 Example: 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

22 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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.


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures."

Similar presentations


Ads by Google