Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference.

Slides:



Advertisements
Similar presentations
Mark Dixon, School of Computing SOFT 120Page 1 5. Passing Parameters by Reference.
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 C++ Syntax and Semantics The Development Process.
Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 22 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Web applications: Writing data to Databases using ASP.
Mark Dixon, SoCCE SOFT 136Page 1 11 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –Revise Variables and Conditional execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging & Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Software Development Lifecycle, & User Interface Design.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Control Arrays & Container Controls.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Mark Dixon, SoCCE SOFT 131Page 1 25 – Soft 131 Examination Revision.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging, Sound, and Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
ME 142 Engineering Computation I Using Subroutines Effectively.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures.
ME 142 Engineering Computation I Using Subroutines Effectively.
1 Writing Software Kashef Mughal. 2 Algorithms  The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  An Algorithm.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Procedures.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
Mark Dixon, SoCCE SOFT 131Page 1 25 – Soft 131 Examination Revision.
B065: PROGRAMMING Variables 2. Starter  What is wrong with the following piece of code: Option Explicit Off Dim num1 as string Dim num2 as integer num1.
Week 1 Lecture 1 Slide 1 CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton CP2028 Visual Basic Programming 2 v Week.
Making Interactive Programs with Visual Basic .NET
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Chapter # 2 Part 2 Programs And data
A variable is a name for a value stored in memory.
11 – Enumerated Data Types, & Procedure Parameters
DATA HANDLING.
Data Types List Box Combo Box Checkbox Option Box Visual Basic 6.0
Chapter # 2 Part 2 Programs And data
Intro to Programming Concepts
Presentation transcript:

Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference

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

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)

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

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"

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

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

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

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

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

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 (this is the address of the first byte of data allocated to the variable x) 23xInteger Identifier Value Type Memory

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

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

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

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

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

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

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?

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?

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

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