11 – Enumerated Data Types, & Procedure Parameters

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, SoCCE SOFT 131Page 1 09 – Procedures.
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 07 – Iterative Execution.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
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.
Visual Basic: An Object Oriented Approach 10 - Polymorphism.
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.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Procedures.
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 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon Page 1 13 – Object Oriented Programming.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Control Arrays & Container Controls.
Chapter 41 Sub Procedures, Part II (Continue). Chapter 42 Local Variable A variable declared inside a Sub procedure with a Dim statement Space reserved.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
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.
Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical.
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.
Types and Loops.
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.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
Week 4.  Recap – Ranges  For Each Loops  Ranges Referencing Range Objects  Set (keyword)
Arrays1 From time to time an object (a variable, a picture, a label or a command) does not serve as well as a set of objects of a similar kind addressed.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
ME 142 Engineering Computation I Using Subroutines Effectively.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures.
ME 142 Engineering Computation I Using Subroutines Effectively.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
Mark Dixon Page 1 15 – Structured Programming. Mark Dixon Page 2 Admin: Coursework 3 – Test In class test –9 Feb 2010 –2 Feb 2010: revision (technique)
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 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed.
1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and.
B065: PROGRAMMING Variables 2. Starter  What is wrong with the following piece of code: Option Explicit Off Dim num1 as string Dim num2 as integer num1.
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.
Scope. Scope of Names In a small program written by one person, it is easy to be sure that each variable has a unique name In a large program, or one.
Mark Dixon, SoCCE SOFT 136Page 1 08 – Arrays & Structures.
A variable is a name for a value stored in memory.
15 – Structured Programming
2. Understanding VB Variables
Method Mark and Lyubo.
Department Array in Visual Basic
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Process/ Instruction Writing
SEMAFOR.
Data Types List Box Combo Box Checkbox Option Box Visual Basic 6.0
08 – Iterative Execution.
B065: PROGRAMMING Variables 2.
Introduction to Computer Programming IT-104
Presentation transcript:

11 – Enumerated Data Types, & Procedure Parameters

Session Aims & Objectives Aims, to introduce: the idea of enumerated data types passing parameters/arguments to procedures Objectives, after this week’s sessions, you should be able to: declare and use an enumerated data type use parameters in your programs to make procedures more flexible

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"

Example: Curry v1 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 Curry v1

Example: Curry v2 Curry v2 Private Sub lstCurry_Click() 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 Curry v2

Example: Curry v3 Curry v3 Const Mild = 0 Const Medium = 1 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 Curry v3

Example: Curry v4 Curry v4 Enum TSpice Mild = 0 Medium = 1 Hot = 2 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 Curry v4

Exercise: 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

Parameters/Arguments (what) Sometimes procedures need information Making a cup of tea: milk, and number of sugars Makes them more flexible Make cup of tea Milk Sugars

Parameters (how) Procedure Declaration Procedure Call Option Explicit Dim res As Long Private Sub Calc(Num1 As Long, Num2 As long) res = Num1 * Num2 End Sub Private Sub btnCalc_Click() Calc 5, 2 Calc 11, 15 Parameter Animation Procedure Declaration Formal parameters define name & type Procedure Call actual parameters list values in order

Example: House v1 House v1 Option Explicit Private Sub btnDraw_Click() picHouse.Cls picHouse.Line (1000, 1000)-(2000, 1750), , B picHouse.Line (1000, 1000)-(1500, 750) picHouse.Line -(2000, 1000) End Sub House v1

Example: House v1 to v2 House v1 House v2 Option Explicit Private Sub btnDraw_Click() picHouse.Cls picHouse.Line (1000, 1000)-(2000, 1750), , B picHouse.Line (1000, 1000)-(1500, 750) picHouse.Line -(2000, 1000) End Sub Option Explicit Private Sub DrawHouse() picHouse.Line (1000, 1000)-(2000, 1750), , B picHouse.Line (1000, 1000)-(1500, 750) picHouse.Line -(2000, 1000) End Sub Private Sub btnDraw_Click() picHouse.Cls DrawHouse

Example: House v2 House v2 Option Explicit Private Sub DrawHouse() picHouse.Line (1000, 1000)-(2000, 1750), , B picHouse.Line (1000, 1000)-(1500, 750) picHouse.Line -(2000, 1000) End Sub Private Sub btnDraw_Click() picHouse.Cls DrawHouse House v2

Example: House v3 House v3 Option Explicit Private Sub DrawHouse(x As Single, y As Single) picHouse.Line (x, y)-(x + 1000, y + 750), , B picHouse.Line (x, y)-(x + 500, y - 250) picHouse.Line -(x + 1000, y) End Sub Private Sub btnDraw_Click() picHouse.Cls DrawHouse 1000, 1000 DrawHouse 2500, 1000 DrawHouse 4000, 1000 House v3

Exercise: Parameters Given the following declaration: What will the following put in lblOutput? Sub thing(Num1 As Long, Num2 As Long, Num3 As Long) Dim tmpOutput As Long tmpOutput = (Num1 + Num2) * Num3 lblOutput.Caption = tmpOutput End Sub thing 2, 3, 6 thing 6, 3, 2 thing 20, 5, 2 30 18 50

Example: Till v1 Till v1 Option Explicit Dim SubTotal As Double Dim Total As Double Private Sub btnEquals_Click() Dim price As Double Dim quantity As Double price = Val(Me.txtPrice.Text) quantity = Val(Me.txtQuantity.Text) SubTotal = price * quantity lblSubTotal.Caption = "£" & SubTotal End Sub Private Sub btnAdd_Click() Total = Total + SubTotal lblTotal.Caption = "£" & Total Private Sub btnUndo_Click() Total = Total - SubTotal Till v1

Example: Till v2 amount: Double TotalAdd Till v2 Option Explicit Dim SubTotal As Double Dim Total As Double Private Sub TotalAdd(amount As Double) Total = Total + amount Me.lblTotal.Caption = "£" & Total End Sub Private Sub btnEquals_Click() Dim price As Double Dim quantity As Double price = Val(Me.txtPrice.Text) quantity = Val(Me.txtQuantity.Text) SubTotal = price * quantity lblSubTotal.Caption = "£" & SubTotal Private Sub btnAdd_Click() TotalAdd SubTotal Private Sub btnUndo_Click() TotalAdd -SubTotal amount: Double TotalAdd Till v2

Example: Heart Rate v3 Option Explicit Dim HR(0 To 6) As Long Private Sub Form_Load() HR(0) = 134 HR(1) = 127 HR(2) = 139 HR(3) = 155 HR(4) = 143 HR(5) = 151 HR(6) = 141 End Sub Private Sub DrawHR() Dim i As Long picMain.Cls For i = 0 To 6 picMain.Line -(i * 500, HR(i) * 10) Next Private Sub btnDraw_Click() DrawHR

Example: Heart Rate v4 Option Explicit Dim HR(0 To 6) As Long Private Sub Form_Load() HR(0) = 134 HR(1) = 127 HR(2) = 139 HR(3) = 155 HR(4) = 143 HR(5) = 151 HR(6) = 141 End Sub Private Sub DrawHR(picTemp As PictureBox) Dim i As Long picTemp.Cls For i = 0 To 6 picTemp.Line -(i * 500, HR(i) * 10) Next Private Sub btnDraw_Click() DrawHR Me.picMain1 DrawHR Me.picMain2