1 / 19

11 – Enumerated Data Types, & Procedure Parameters

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

kana
Télécharger la présentation

11 – Enumerated Data Types, & Procedure Parameters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 11 – Enumerated Data Types, & Procedure Parameters

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

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

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

  10. Parameters (how) 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 End Sub • Procedure Declaration • Formal parameters define name & type • Procedure Call • actual parameters list values in order Parameter Animation

  11. Example: 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

  12. 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 House v1 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 DrawHouse DrawHouse End Sub

  13. Example: 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 DrawHouse DrawHouse End Sub House v2

  14. Example: 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 End Sub House v3

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

  16. Example: 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 End Sub Private Sub btnUndo_Click() Total = Total - SubTotal lblTotal.Caption = "£" & Total End Sub Till v1

  17. Example: 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 End Sub Private Sub btnAdd_Click() TotalAdd SubTotal End Sub Private Sub btnUndo_Click() TotalAdd -SubTotal End Sub TotalAdd amount: Double Till v2

  18. 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 End Sub Private Sub btnDraw_Click() DrawHR End Sub

  19. 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 End Sub Private Sub btnDraw_Click() DrawHR Me.picMain1 DrawHR Me.picMain2 End Sub

More Related