Upload
raheem
View
34
Download
1
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
Mark Dixon, SoCCE SOFT 131 Page 1
12 – Enumerated Data-Types & Pass-by-reference
Mark Dixon, SoCCE SOFT 131 Page 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
Mark Dixon, SoCCE SOFT 131 Page 3
Project Files• Ensure that you submit:
– project file (*.vbp)– all form files (*.frm, and *.frx)– all module files (*.bas)
Mark Dixon, SoCCE SOFT 131 Page 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
Mark Dixon, SoCCE SOFT 131 Page 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"
Mark Dixon, SoCCE SOFT 131 Page 6
Example 1: 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
Mark Dixon, SoCCE SOFT 131 Page 7
Example 2: 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
Mark Dixon, SoCCE SOFT 131 Page 8
Example 3: 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
Mark Dixon, SoCCE SOFT 131 Page 9
Example 4: 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
Mark Dixon, SoCCE SOFT 131 Page 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 = 2End Enum
Enum TPublication Book = 0 Journal = 1End Enum
Mark Dixon, SoCCE SOFT 131 Page 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)
23x IntegerIdentifier
Value TypeMemory
0
63542
Mark Dixon, SoCCE SOFT 131 Page 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 definitionSub p2(ByRef y As integer) …End Sub
Mark Dixon, SoCCE SOFT 131 Page 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
Mark Dixon, SoCCE SOFT 131 Page 14
Example: Change the ValueDim a As IntegerDim 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 = 11b = 12P1 a ' What is the value of a?P2 b ' What is the value of b?
Pass by Ref
Mark Dixon, SoCCE SOFT 131 Page 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.
Mark Dixon, SoCCE SOFT 131 Page 16
Example: Pass by Ref vs. FunctionSub P2(x As Integer) x = x * 2End Sub
Function F2(x As integer) As Integer F2 = x * 2End 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?
P1var x: integer
F1x: integer integer
Mark Dixon, SoCCE SOFT 131 Page 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
Mark Dixon, SoCCE SOFT 131 Page 18
Example: TotalDim Nums(1 To 5) As IntegerDim 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) = 23Nums(2) = 17Nums(3) = 28Nums(4) = 12Nums(5) = 25tot = Total() ' What is the value of tot?
Mark Dixon, SoCCE SOFT 131 Page 19
Example: AverageDim 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?
Mark Dixon, SoCCE SOFT 131 Page 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
Mark Dixon, SoCCE SOFT 131 Page 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?
TotAvevar T: integervar A: double