12 Enumerated DataTypes Passbyreference Mark Dixon So CCE
12 – Enumerated Data-Types & Pass-by-reference Mark Dixon, So. CCE SOFT 131 Page 1
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 Mark Dixon, So. CCE SOFT 131 Page 2
Project Files • Ensure that you submit: – project file (*. vbp) – all form files (*. frm, and *. frx) – all module files (*. bas) Mark Dixon, So. CCE SOFT 131 Page 3
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 Mark Dixon, So. CCE SOFT 131 Page 4
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" Mark Dixon, So. CCE SOFT 131 Page 5
Example 1: Curry v 1 Option Explicit Private Sub Form_Load() lst. Curry. Add. Item "Mild", 0 lst. Curry. Add. Item "Medium", 1 lst. Curry. Add. Item "Hot", 2 pic. Curry. Fill. Style = vb. Solid End Sub Private Sub lst. Curry_Click() lbl. Curry. Code. Caption = lst. Curry. List. Index lbl. Curry. Text. Caption = lst. Curry. List(lst. Curry. L If lst. Curry. List. Index = 0 Then pic. Curry. Fill. Color = vb. White Else. If lst. Curry. List. Index = 1 Then pic. Curry. Fill. Color = vb. Yellow Else pic. Curry. Fill. Color = vb. Red End If pic. Curry. Cls pic. Curry. Circle (1000, 750), 500 End Sub Mark Dixon, So. CCE SOFT 131 Page 6
Example 2: Curry v 2 Option Explicit Private Sub Form_Load() lst. Curry. Add. Item "Mild", 0 lst. Curry. Add. Item "Medium", 1 lst. Curry. Add. Item "Hot", 2 pic. Curry. Fill. Style = vb. Solid End Sub Mark Dixon, So. CCE Private Sub lst. Curry_Click() Dim Cu. Co As Long ' Curry code Cu. Co = lst. Curry. List. Index lbl. Curry. Code. Caption = Cu. Co lbl. Curry. Text. Caption = lst. Curry. List(Cu. Co) If Cu. Co = 0 Then pic. Curry. Fill. Color = vb. White Else. If Cu. Co = 1 Then pic. Curry. Fill. Color = vb. Yellow Else pic. Curry. Fill. Color = vb. Red End If pic. Curry. Cls pic. Curry. Circle (1000, 750), 500 End Sub SOFT 131 Page 7
Example 3: Curry v 3 Option Explicit Const Mild = 0 Const Medium = 1 Const Hot = 2 Private Sub Form_Load() lst. Curry. Add. Item "Mild", Mild lst. Curry. Add. Item "Medium", Medium lst. Curry. Add. Item "Hot", Hot pic. Curry. Fill. Style = vb. Solid End Sub Private Sub lst. Curry_Click() Dim Cu. Co As Long ' Curry code Cu. Co = lst. Curry. List. Index lbl. Curry. Code. Caption = Cu. Co lbl. Curry. Text. Caption = lst. Curry. List(Cu. Co) If Cu. Co = Mild Then pic. Curry. Fill. Color = vb. White Else. If Cu. Co = Medium Then Mark Dixon, So. CCE SOFT 131 pic. Curry. Fill. Color = vb. Yellow Else pic. Curry. Fill. Color = vb. Red End If pic. Curry. Cls pic. Curry. Circle (1000, 750), 500 End Sub Page 8
Example 4: Curry v 4 Option Explicit Enum TSpice Mild = 0 Medium = 1 Hot = 2 End Enum Private Sub Form_Load() lst. Curry. Add. Item "Mild", Mild lst. Curry. Add. Item "Medium", Medium lst. Curry. Add. Item "Hot", Hot pic. Curry. Fill. Style = vb. Solid End Sub Private Sub lst. Curry_Click() Dim Cu. Co As TSpice ' Curry code Cu. Co = lst. Curry. List. Index lbl. Curry. Code. Caption = Cu. Co lbl. Curry. Text. Caption = lst. Curry. List(Cu. Co) If Cu. Co = Mild Then pic. Curry. Fill. Color = vb. White Else. If Cu. Co = Medium Then pic. Curry. Fill. Color = vb. Yellow Else pic. Curry. Fill. Color = vb. Red End If pic. Curry. Cls pic. Curry. Circle (1000, 750), 500 End Sub Mark Dixon, So. CCE SOFT 131 Page 9
Exercise 1: EDTs • Create an EDT to store the following classification of height: short, average, tall Enum THeight Short = 0 Average = 1 Tall = 2 End Enum • Create an EDT to store the following classification of publication: book, journal Enum TPublication Book = 0 Journal = 1 End Enum Mark Dixon, So. CCE SOFT 131 Page 10
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: 0 63542 x 23 Integer Identifier Value Memory Type • 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) Mark Dixon, So. CCE SOFT 131 Page 11
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 p 1(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 p 2(By. Ref y As integer) … End Sub Mark Dixon, So. CCE SOFT 131 Page 12
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 Mark Dixon, So. CCE SOFT 131 Page 13
Example: Change the Value Dim a As Integer Dim b As integer Sub P 1(x As integer) x = x * 2 End Sub Pass by Ref Sub P 2(By. Ref x AS integer) x = x * 2 End Sub a = 11 b = 12 P 1 a P 2 b Mark Dixon, So. CCE ' What is the value of a? ' What is the value of b? SOFT 131 Page 14
What can be passed • Pass by value – both literals and variables can be passed (variables are substituted by their value) p 1 y p 1 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 p 2 y p 2 21 Mark Dixon, So. CCE ' This is fine. ' This generates an error. SOFT 131 Page 15
Example: Pass by Ref vs. Function Sub P 2(x As Integer) x = x * 2 End Sub var x: integer Function F 2(x As integer) As Integer F 2 = x * 2 End Function F 1 x: integer P 1 integer Dim b As integer b = 4 P 2 b ' What is the value of b? b = 4 b = F 2(b) ' What is the value of b? Mark Dixon, So. CCE SOFT 131 Page 16
Pass by Ref vs. Function • a procedure that changes the value of a single parameter is equivalent to a function, – the procedure P 2: P 2 b was equivalent to: – the function F 2: b = F 2(b) • However, – F 2 is far more explicit, – P 2 is a bit cryptic: not obvious that value of b changes • this makes code difficult to read, which can lead to errors Mark Dixon, So. CCE SOFT 131 Page 17
Example: Total Dim Nums(1 To 5) As Integer Dim tot As integer Function Total() As Integer Dim tmp. Tot As integer Dim i As integer tmp. Tot = 0 For i = 1 to 5 tmp. Tot = tmp. Tot + Nums(i) Next Total = tmp. Tot End Function Nums(1) = 23 Nums(2) = 17 Nums(3) = 28 Nums(4) = 12 Nums(5) = 25 tot = Total() Mark Dixon, So. CCE ' What is the value of tot? SOFT 131 Page 18
Example: Average Dim ave As Double Function Average() As Double Dim tmp. Tot As Integer Dim i As Integer tmp. Tot = 0 for i = 1 to 5 tmp. Tot = tmp. Tot + Nums(i) Next Average = tmp. Tot / 5 End Function ave = Average() Mark Dixon, So. CCE ' What is the value of ave? SOFT 131 Page 19
Two results? • Total and Average functions share a lot of code • Useful to combine them • Problem: – a function can only have 1 output – This: Tot. Ave integer double (total) (average) is not possible (in VB, or Delphi anyway) Mark Dixon, So. CCE SOFT 131 Page 20
Example: Total and Average Sub Tot. Ave(By. Ref T As Integer, _ By. Ref 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 Tot. Ave tot, ave Mark Dixon, So. CCE var T: integer var A: double Tot. Ave ' What is the value of ave and tot? SOFT 131 Page 21
- Slides: 21