Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

33
Mark Dixon, SoCCE SOFT 131 Page 1 10 – Enumerated Data Types & Procedure Parameters
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    223
  • download

    1

Transcript of Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Page 1: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 1

10 – Enumerated Data Types & Procedure Parameters

Page 2: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 2

Session Aims & Objectives• Aims, to introduce:

– the idea of enumerated data types– passing parameters/arguments to procedures– control arrays

• 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– use control arrays

Page 3: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

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 10 – Enumerated Data Types & Procedure Parameters.

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 10 – Enumerated Data Types & Procedure Parameters.

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 10 – Enumerated Data Types & Procedure Parameters.

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 10 – Enumerated Data Types & Procedure Parameters.

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 10 – Enumerated Data Types & Procedure Parameters.

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 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 9

Parameters in Event Procedures• Provide us with information about event

– e.g. if mouse was clicked• where was it (x, and y co-ordinates)• which mouse button was pressed• which keyboard buttons were pressed ([Shift] / [Ctrl])

Page 10: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 10

Example: Draw

Option Explicit

Private Sub picDraw_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then picDraw.PSet (X, Y) End IfEnd Sub

Private Sub picDraw_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then picDraw.Line -(X, Y) End IfEnd Sub

• Parameters:– appear in the brackets of a procedure definition

horizontal & verticalmouse co-ordinates

mousebutton

keyboard[Shift] [Ctrl] keys

Page 11: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 11

Parameters/Arguments (what)• Sometimes our own procedures need

information– Making a cup of tea:

• milk, and number of sugars

• Makes them more flexible

Make cupof tea

Milk

Sugars

Page 12: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 12

Parameters (how)• Procedure Declaration

– Formal parameters define name & type

• Procedure Call– actual parameters list values in order

Option ExplicitDim 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 End Sub

Page 13: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 13

Parameters (how)

Page 14: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 14

Example: House v1Option 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

Page 15: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 15

Example: House v1 to 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

DrawHouse

DrawHouse

End Sub

House v1 House v2

Page 16: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 16

Example: House v2Option 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

DrawHouse

DrawHouse

End Sub

House v2

Page 17: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 17

Example: House v3Option 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

End Sub

House v3

Page 18: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 18

Questions: 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 = tmpOutputEnd Sub

thing 2, 3, 6thing 6, 3, 2thing 20, 5, 2

301850

Page 19: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 19

Example: Till v1Option 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

End Sub

Private Sub btnUndo_Click()

Total = Total - SubTotal

lblTotal.Caption = "£" & Total

End Sub

Till v1

Page 20: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 20

amount: Double

Example: Till v2Option ExplicitDim SubTotal As DoubleDim 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 End Sub

Private Sub btnAdd_Click() TotalAdd SubTotal End Sub

Private Sub btnUndo_Click() TotalAdd -SubTotal End Sub

TotalAdd

Till v2

Page 21: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 21

Example: Heart Rate v3Option ExplicitDim 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) = 141End Sub

Private Sub DrawHR()Dim i As Long picMain.Cls For i = 0 To 6 picMain.Line -(i * 500, HR(i) * 10) NextEnd Sub

Private Sub btnDraw_Click() DrawHREnd Sub

Page 22: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 22

Example: Heart Rate v4Option ExplicitDim 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) = 141End 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) NextEnd Sub

Private Sub btnDraw_Click() DrawHR Me.picMain1 DrawHR Me.picMain2End Sub

Page 23: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 23

Control Arrays• Several controls have same name

• Distinguished by unique index

• Very similar to a variable array

• Makes it easier to do same thing to all controls – using for loop

Page 24: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 24

Example: Ships - UI

lblMooringID

picMain(3)

picMain(1)

picMain(2)

picMain(0)

• Four controls share same name (picMain):

Ships

Page 25: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 25

Example: Ships - CodeOption Explicit

Sub Draw()Dim i As Long For i = 0 To 3 picMain(i).DrawWidth = 3 picMain(i).Line (1000, 1000)-(2000, 1000) picMain(i).Line -Step(-200, 200) picMain(i).Line -Step(-600, 0) picMain(i).Line -Step(-200, -200) NextEnd Sub

Private Sub picMain_Click(Index As Integer) lblMooringID.Caption = Index DrawEnd Sub

Page 26: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 26

Control Arrays - How• To create a control array, either:

– Create a control with the same name as an existing control (VB will ask you if you want to create a control array)

Or:– Create a control and type a value into its index

property (e.g. 0)

Page 27: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 27

Container Controls• Some controls can contain other controls:

– Picture boxes– Frames

• The container (parent) control clips the contained (child) controls– The child controls will not be displayed outside

the boundary of their parent

Page 28: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 28

Example – Pic Container• The inner picture box is clipped:

picInner

picOuter

Page 29: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 29

Tutorial Exercise: 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 30: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 30

Tutorial Exercise: Draw• LEARNING OBJECTIVE:

– use parameters provided by event procedures

• Task 1: Get the Draw example (from the lecture) working.• Task 2: Modify your code – allow the user to rub out (paint

in the background colour) using the right mouse button.• Task 3: Modify your code – allow the user to use a thicker

line by holding down the [ctrl] button on the keyboard

Page 31: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 31

Tutorial Exercise: House• LEARNING OBJECTIVE:

– use parameters in your own procedures, to make them more flexible (general purpose)

• Task 1: Get the House examples (versions 1, 2, and 3) from the lecture working.

• Task 2: Modify your code – add code that draws a door and a window for each house.

• Task 3: Modify your code – add code that draws 10 small houses. Hint: use a for loop (rather than simply copying the procedure calls)

Page 32: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 32

Tutorial Exercise: Till• LEARNING OBJECTIVE:

– use parameters in your own procedures, to make them more flexible (general purpose)

• Task 1: Get the Till examples (versions 1 and 2) from the lecture working.

• Task 2: Modify your code – add code that clears all data when the clear button is clicked.

• Task 3: Modify your code – create a procedure to calculate and display the sub total.

Page 33: Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.

Mark Dixon, SoCCE SOFT 131 Page 33

Tutorial Exercise: Heart Rate• LEARNING OBJECTIVE:

– use parameters in your own procedures, to make them more flexible (general purpose)

• Task 1: Get the Heart Rate examples (versions 3 and 4) from the lecture working.

• Task 2: Modify your code – use additional parameters to control the horizontal and vertical sizes of the graphs (so that the two graphs can be different sizes).