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

Slides:



Advertisements
Similar presentations
Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Oriented Analysis, Design, and Programming.
Advertisements

Mark Dixon, SoCCE SOFT 131Page 1 22 – Object Oriented Analysis, Design, and Programming.
Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon Page 1 04 – Database Design: Forms. Mark Dixon Page 2 Session Aims & Objectives Aims –To allow easier data entry using forms Objectives, by.
Mark Dixon Page 1 22 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Web applications: Writing data to Databases using ASP.
Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 136Page 1 11 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –Revise Variables and Conditional execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging & Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon Page 1 21 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Software Development Lifecycle, & User Interface Design.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Control Arrays & Container Controls.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon Page 1 20 – Web applications: Writing data to Databases using ASP.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Mark Dixon Page 1 15 – Web applications: Server-side code (ASP)
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
CSI 101 Spring 2009 Review and Recap of Visual Basic Wednesday, April 29 th.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP.
Mark Dixon Page 1 10 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of code to declare a variable called h Write a line of.
Mark Dixon Page 1 19 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
Mark Dixon 1 07 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
1/36 Database Programming with Visual Basic.Net and MS Access IKE Lab. Yunho Song Database Management and Analysis.
Mark Dixon Page 1 09 – Arrays. Mark Dixon Page 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
Mark Dixon Page 1 21 – Web applications: Writing data to Databases using ASP.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon SOFT 131Page 1 12 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Databases: Structured Query Language.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Procedures.
Mark Dixon 1 15 – Structured Programming. Mark Dixon 2 Admin: Test 2 In class test –11 Feb 2014 –4 Feb 2014: revision (technique) session 50 mins short.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
Mark Dixon, SoCCE SOFT 131Page 1 25 – Soft 131 Examination Revision.
1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and.
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Mark Dixon, SoCCE SOFT 136Page 1 08 – Arrays & Structures.
11 – Enumerated Data Types, & Procedure Parameters
Department Array in Visual Basic
Data Types List Box Combo Box Checkbox Option Box Visual Basic 6.0
8 Records 25/07/2019.
Presentation transcript:

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

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

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"

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

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

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

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

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

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

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

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

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 Forenames: string Salaries: double

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

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

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"

Mark Dixon, SoCCE SOFT 131Page 16 Structures: Pets

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"

Mark Dixon, SoCCE SOFT 131Page 18 Array of Structures: Pets

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"

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"

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 Salary: double Forenames: string each row is a TEmployee makes it easier to get details of single employee

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

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.

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

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.