Download presentation
Presentation is loading. Please wait.
1
Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference
2
Mark Dixon, SoCCE SOFT 131Page 2 Assignment Object naming conventions: txt – text box btn – command button pic – picture box Appropriate use of: –indentation –remarks –constants (no magic numbers) –variables (module level, and local) –procedures/functions –arrays –structures –enumerate data types
3
Mark Dixon, SoCCE SOFT 131Page 3 Project Files Ensure that you submit: –project file (*.vbp) –all form files (*.frm, and *.frx) –all module files (*.bas)
4
Mark Dixon, SoCCE SOFT 131Page 4 Session Aims & Objectives Aims –To introduce the idea of enumerated data types –To introduce the idea of passing by reference Objectives, by end of this week’s sessions, you should be able to: –declare and use an enumerated data type –pass parameters by reference
5
Mark Dixon, SoCCE SOFT 131Page 5 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" Alternatively, use numbers to represent text: 1"mild" 2"medium" 3"hot"
6
Mark Dixon, SoCCE SOFT 131Page 6 Example 1: 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
7
Mark Dixon, SoCCE SOFT 131Page 7 Example 2: 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
8
Mark Dixon, SoCCE SOFT 131Page 8 Example 3: 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
9
Mark Dixon, SoCCE SOFT 131Page 9 Example 4: 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
10
Mark Dixon, SoCCE SOFT 131Page 10 Exercise 1: 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
11
Mark Dixon, SoCCE SOFT 131Page 11 Variables and Memory Addresses The computer keeps track of where variables are stored in memory by using memory addresses. Every byte (position) in memory has a memory address: In the above example the variable identified by the name x is stored at location 63542 (this is the address of the first byte of data allocated to the variable x) 23xInteger Identifier Value Type Memory 0 63542
12
Mark Dixon, SoCCE SOFT 131Page 12 Parameter Passing Methods There are 2 ways to pass parameters to functions and procedures: –Passing by Value: a literal value is passed from the call to the definition (you have already used this) Sub p1(x As integer) … End Sub –Passing by Reference: a variable’s memory address (a reference to the variables position in memory) is passed from the call to the definition Sub p2(ByRef y As integer) … End Sub
13
Mark Dixon, SoCCE SOFT 131Page 13 Why pass by reference? It allows the value of the passed variable to be changed –i.e. it allows functions and procedures to change the value of things passed to them Normally parameters are for input data – only functions can output data via the return value Pass by reference allows data to be input and output via parameters
14
Mark Dixon, SoCCE SOFT 131Page 14 Example: Change the Value Dim a As Integer Dim b As integer Sub P1(x As integer) x = x * 2 End Sub Sub P2(ByRef x AS integer) x = x * 2 End Sub a = 11 b = 12 P1 a' What is the value of a? P2 b' What is the value of b? Pass by Ref
15
Mark Dixon, SoCCE SOFT 131Page 15 What can be passed Pass by value – both literals and variables can be passed (variables are substituted by their value) p1 y ' This is fine. p1 21 ' This is fine. Pass by reference – only variables can be passed (in fact the variable’s memory address is passed) literals cannot be passed – they have no memory address p2 y ' This is fine. p2 21 ' This generates an error.
16
Mark Dixon, SoCCE SOFT 131Page 16 Example: Pass by Ref vs. Function Sub P2(x As Integer) x = x * 2 End Sub Function F2(x As integer) As Integer F2 = x * 2 End Function Dim b As integer b = 4 P2 b' What is the value of b? b = 4 b = F2(b)' What is the value of b? P1 var x: integer F1 x: integerinteger
17
Mark Dixon, SoCCE SOFT 131Page 17 Pass by Ref vs. Function a procedure that changes the value of a single parameter is equivalent to a function, –the procedure P2: P2 b was equivalent to: –the function F2: b = F2(b) However, –F2 is far more explicit, –P2 is a bit cryptic: not obvious that value of b changes this makes code difficult to read, which can lead to errors
18
Mark Dixon, SoCCE SOFT 131Page 18 Example: Total Dim Nums(1 To 5) As Integer Dim tot As integer Function Total() As Integer Dim tmpTot As integer Dim i As integer tmpTot = 0 For i = 1 to 5 tmpTot = tmpTot + Nums(i) Next Total = tmpTot End Function Nums(1) = 23 Nums(2) = 17 Nums(3) = 28 Nums(4) = 12 Nums(5) = 25 tot = Total()' What is the value of tot?
19
Mark Dixon, SoCCE SOFT 131Page 19 Example: Average Dim ave As Double Function Average() As Double Dim tmpTot As Integer Dim i As Integer tmpTot = 0 for i = 1 to 5 tmpTot = tmpTot + Nums(i) Next Average = tmpTot / 5 End Function ave = Average()' What is the value of ave?
20
Mark Dixon, SoCCE SOFT 131Page 20 Two results? Total and Average functions share a lot of code Useful to combine them Problem: –a function can only have 1 output –This: is not possible (in VB, or Delphi anyway) double (average) integer (total) TotAve
21
Mark Dixon, SoCCE SOFT 131Page 21 Example: Total and Average Sub TotAve(ByRef T As Integer, _ ByRef A As Double) Dim I As integer T = 0 For i = 1 to 5 T = T + Nums(i) Next A = T / 5 End Sub tot = 12 ave = 15 TotAve tot, ave ' What is the value of ave and tot? TotAve var T: integer var A: double
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.