1 / 21

12 – Enumerated Data-Types & Pass-by-reference

12 – Enumerated Data-Types & Pass-by-reference. 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

raheem
Download Presentation

12 – Enumerated Data-Types & Pass-by-reference

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. 12 – Enumerated Data-Types & Pass-by-reference

  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. Project Files • Ensure that you submit: • project file (*.vbp) • all form files (*.frm, and *.frx) • all module files (*.bas)

  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. 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. 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. 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. 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. 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. 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. 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) 0 63542 x 23 Integer Identifier Type Value Memory

  12. Parameter Passing Methods • There are 2 ways to pass parameters to functions and procedures: • Passing by Value: a literalvalue 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 definitionSub p2(ByRef y As integer) …End Sub

  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. 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 P1a ' What is the value of a? P2b ' What is the value of b? Pass by Ref

  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 addressp2 y ' This is fine. p2 21 ' This generates an error. 

  16. Example: Pass by Ref vs. Function P1 var x: integer F1 x: integer integer Sub P2(x AsInteger) x = x * 2 End Sub Function F2(x As integer) AsInteger F2 = x * 2 End Function Dim b As integer b = 4 P2b ' What is the value of b? b = 4 b = F2(b) ' What is the value of b?

  17. Pass by Ref vs. Function • a procedure that changes the value of a single parameter is equivalent to a function, • the procedure P2:P2bwas 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. Example: Total Dim Nums(1 To 5)AsInteger Dim tot As integer Function Total() AsInteger Dim tmpTot As integer Dimi 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. Example: Average Dim ave AsDouble Function Average() AsDouble Dim tmpTot AsInteger Dim i AsInteger 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. Two results? integer (total) TotAve double (average) • 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)

  21. Example: Total and Average SubTotAve(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? var T: integer TotAve var A: double

More Related