mark dixon, socce soft 131page 1 18 – enumerated data types and arrays of structures

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

Upload: frank-parker

Post on 20-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

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

Mark Dixon, SoCCE SOFT 131 Page 1

18 – Enumerated Data Types andArrays of Structures

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

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

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

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

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

Mark Dixon, SoCCE SOFT 131 Page 4

Example: Curry v1Option Explicit

Private Sub Form_Load() lstCurry.AddItem "Mild", 0 lstCurry.AddItem "Medium", 1 lstCurry.AddItem "Hot", 2 picCurry.FillStyle = vbSolidEnd 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), 500End Sub

Curry v1

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

Mark Dixon, SoCCE SOFT 131 Page 5

Example: Curry v2Option Explicit

Private Sub Form_Load() lstCurry.AddItem "Mild", 0 lstCurry.AddItem "Medium", 1 lstCurry.AddItem "Hot", 2 picCurry.FillStyle = vbSolidEnd 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), 500End Sub

Curry v2

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

Mark Dixon, SoCCE SOFT 131 Page 6

Example: Curry v3Option Explicit

Const Mild = 0Const Medium = 1Const 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

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

Mark Dixon, SoCCE SOFT 131 Page 7

Example: Curry v4Option Explicit

Enum TSpice Mild = 0 Medium = 1 Hot = 2End 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

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

Mark Dixon, SoCCE SOFT 131 Page 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 = 2End Enum

Enum TPublication Book = 0 Journal = 1End Enum

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

Mark Dixon, SoCCE SOFT 131 Page 9

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

– e.g.• surname• forenames• date of birth• address• telephone number• salary

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

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

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

Mark Dixon, SoCCE SOFT 131 Page 11

Example: Code Design• 2 layers:

Layer 1Event Handler

Procedures

Layer 2General

Procedures

btnPreviousClick

btnNextClick

FormLoad

EmployeeDisplay

EmployeeStore

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

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

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

Mark Dixon, SoCCE SOFT 131 Page 13

Example: Employees v1Option ExplicitDim Surnames(1 To 10) As StringDim Forenames(1 To 10) As StringDim Salaries(1 To 10) As DoubleDim 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 EmpDisplayEnd Sub

Private Sub btnNext_Click() EmpStore curEmp = curEmp + 1 EmpDisplayEnd Sub

Employees v1

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

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

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

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

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

Mark Dixon, SoCCE SOFT 131 Page 16

Structures: Pets

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

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

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

Mark Dixon, SoCCE SOFT 131 Page 18

Array of Structures: Pets

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

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

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

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

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

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

each row isa TEmployee

• makes it easier to get details of single employee

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

Mark Dixon, SoCCE SOFT 131 Page 22

Example: Employees v2Option Explicit

Private Type TEmployee Surname As String Forenames As String Salary As DoubleEnd Type

Dim Employees(1 To 10) As TEmployeeDim curEmp As Integer

Sub EmpDisplay() lblEmpNum.Caption = curEmp txtSurname.Text = Employees(curEmp).Surname txtForenames.Text = Employees(curEmp).Forenames txtSalary.Text = Employees(curEmp).SalaryEnd 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 EmpDisplayEnd Sub

Private Sub btnNext_Click() EmpStore curEmp = curEmp + 1 EmpDisplayEnd Sub

Employees v2

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

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

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

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

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

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