lec6 p 1 cp2030 visual basic for c++ programmers copyright © university of wolverhampton cp2030...

35
Lec6 P 1 isual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index Procedures and Parameters Control Arrays Debugging

Post on 21-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 1CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

CP2030 VBFCLecture 6

Back To Index

Procedures and Parameters

Control Arrays

Debugging

Page 2: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 2CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Modularity of Code: Procedures

As with C :

Remove duplication of code within the same application

Structured programming

Well designed code procedures can be reused in other applications

– Need to stand alone,The only link to the rest of the application is through passed parameters

– Need to have a specific functionality

Page 3: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 3CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

C uses only functions - used to return just one value or return more than one value via its parameters.

Eg.

Void addnums(float fnum1, float fnum2, float& fresult); Prototpye, two input and one output parameter

addnums(2,5,fresult); Function Call

void addnums(float fnum1, float fnum2, float& fresult) Procedure declaration, & - address of

{

fresult = fnum1 + fnum2;

}

The function return the result via the parameter fresult

Page 4: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 4CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Question

Modify the example to show code to return the result via the functions name - ie. No output paraments to the function.

Answer Students to add the answer

Page 5: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 5CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

VB uses Functions and Sub-Procedures (Subroutines)

– Functions are used to return ONE value

– Subroutines perform some action - also used to return more than one value

– No prototypes are used in VB

Page 6: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 6CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Passing Parameters by Value & Reference

Visual Basic passes parameters by reference as default– This means that if you make a change to a parameter within the called

sub-procedure the change is reflected in the original variable

– It is also possible to pass parameters by value– This means that when you pass the parameter to the sub-procedure it

takes a copy of it, leaving the original parameter unchanged

– It is good practice to pass parameters by value when there is no intention of changing them.

Page 7: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 7CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Parameter Declarations As in C you can declare parameters are either input (Value) or

output (Variable/address of/by reference) Value parameters are declared locally to the Procedure, hence

used for input to a procedure. Reference parameters share the same address as arguments and

are used to return values from procedures (output).

Input : Value

Students to add details Output : Reference

Students to add details

Page 8: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 8CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Modularity of Code: Sub-Procedures

Calling:Call subname(value1, value2, etc)

or

subname value1, value2, etc

Sub-Procedure Structure:Sub subname(value1 As type, Value2 As type, etc)

declarations (local)

statements

End Sub

Arguments

Parameters

Page 9: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 9CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Modularity of Code: Functions

Passes back a value to the calling procedure

Calling– variable = funcname( value1, value2, etc)

Function structure:– Function funcname( value1 As type, value2 As type, etc) As type

statements funcname = returnvalueEnd Function

Note the return type

Page 10: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 10CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Sub-Procedure Locations

Where sub-procedures are located:Form1 Form2 Module1

Event 1

Event 2

Event 3

Event 4

Event 5

Type1Type2Global guA as Type1

Dim iX as Integer

Dim iD as Integer

Dim iY as Integer

Dim iQ as Integer

Dim iZ as Integer

Dim iR as Integer

Dim iS as Integer

Static iE as Integer

Subprocedure SubH

Subprocedure SubGSubprocedure SubF

Page 11: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 11CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Adding a Sub-Procedure or Function -1

Whilst you are in a code window, to add a new procedure into the general declarations section of either a form or a module, select view from the main menu bar

Then select New Procedure... from the sub-menu

Page 12: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 12CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

This will bring up a dialog:

You should select the radio button for either a Sub-Procedure or a Function

You then need to enter the Sub-Procedure’s name

Page 13: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 13CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

This adds a new sub-procedure or function structure definition to the code window

Sub CubeNo ()

End Subor

Function CubeNo ()

End Function

You should then add any parameters required and a return type if it is a function

Page 14: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 14CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Adding Parameters

You can then add in the parameters and code to your function or procedure:

Sub CubeNo (BYVAL lNum As Long, lCube As Long)lCube = lNum * lNum * lNum

End Sub

or

Function CubeNo (BYVAL lNum As Long) As LongCubeNo = lNum * lNum * lNum

End Function

Page 15: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 15CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Adding a Sub-Procedure or Function -2

Type in the Sub or Function and Visual Basic will automatically add the End Sub or End Function

Sub CubeNo ()

End Subor

Function CubeNo ()

End Function

You should then add any parameters required and a return type if it is a function

Page 16: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 16CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Example

Let’s consider a simple program that works out the VAT that is payable on goods

You enter the price, click on Calc and the VAT and total price are calculated

Page 17: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 17CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Solution 1 : Sub-Procedures

Sub-Procedure CalcVAT:Sub CalcVAT (cValue As Currency, cVat As Currency) cVat = cValue * .175End Sub

Calc Command Button:Sub Command1_Click ()Dim cPrice As CurrencyDim cVat As Currency cPrice = Val(Text1.Text) Call CalcVAT(cPrice, cVat) Label1.Caption = cVat Label2.Caption = cPrice + cVatEnd Sub

Poor use of Parameters

Page 18: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 18CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Solution2 : Functions

We can declare CalcVAT as a function instead of a sub-procedure Function CalcVAT:Function CalcVAT(cValue As Currency) As Currency CalcVAT = cValue * .175End Function

Calc Command Button:Sub Command1_Click ()Dim cPrice As CurrencyDim cVat As Currency cPrice = Val(Text1.Text) cVat = CalcVAT(cPrice) Label1.Caption = cVat Label2.Caption = cPrice + cVatEnd Sub

Again Poor use of parameters

Page 19: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 19CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Question

Poor use of parameter declaration in both solutions !

(a) Comment on the poor use of the Parameter declarations in both examples

(b) Re-write the code using more suitable declarations.

Page 20: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 20CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Passing Arrays

eg

Students to add example

Note:

We indicate that it is an array that is being passed to the procedure by putting () after the identifier name

Page 21: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 21CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Calling Procedures & Passing Arrays

Note the brackets after the array name used when an array variable is passed as an argument in the procedure call

Students to add code

Page 22: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 22CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Control Arrays

Visual Basic provides us with arrays of standard data types and user defined types :

Dim inum(20) As Integer

Dim studrec(100) As Studenttype

Visual Basic also provides us with arrays of Controls

We can create an array of Command buttons or List boxes, etc

This enables efficient processing of the controls

Page 23: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 23CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Control Arrays 1

Control Array is a number of controls that have the same event handler, the system passes the controls index into the procedure.

0 1 2

Page 24: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 24CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Control Arrays 2

The Control Array event handler can use a select statement instead of an if statement.

0 1 2

Page 25: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 25CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Control Arrays: Creating

Three ways to create a control array:

– Copy an existing control and paste it onto form

– Assign same name to more than one control

– Set Index property of a control to non-null value

For each of the above Visual Basic will ask do you wish to create a control array

Page 26: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 26CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Creating Control Arrays at run-time:

Load ControlName(Index%)

Unload ControlName(Index%)

Students to add notes on this section

Page 27: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 27CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Demo 1 - control arrays

Create a form with two text fields, a label to hold an answer and a control array of four command buttons.

When a button is pressed the operation & answer should be displayed in Label1: 45 - 52 = -7 or 3 + 5 = 4

Page 28: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 28CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Select Case Statement & Control Arrays

Using the Select Case statement to choose which control within a control array has been pressed:

Sub Command1_Click (Index As Integer)

Dim result As Integer

Students to add code

label1.caption = result

End Sub

Page 29: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 29CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Demo 2 - Labels as a control array

Create array of Labels at design time

Page 30: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 30CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Debugging - Introduction

There are three types of errors :

Compile errors occur as a result of code that is incorrectly constructed including syntax error

– matched control structure, such as a Next statement without a

corresponding For statement, – or programming mistakes that violate the rules of Basic, such as a

misspelled word, a missing separator,– or a type mismatch.– syntax errors include passing an incorrect number of arguments to an

intrinsic function – or forgetting to match parentheses

Page 31: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 31CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

Source Of errors

Run-time errors occur after the application starts to execute. – attempting an illegal operation, such as writing to a file that doesn't exist – or dividing by zero.

Logical errors the program doesn't perform as intended, and produces incorrect results.

Page 32: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 32CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

How to debug - overview

Debugging is a process by which you find and resolve errors in your code.

To debug code in Visual Basic,

1. Print the code, if you find it easier to read code on paper instead of onscreen.

2. Run the application to find trouble spots:– Run until an error stops execution, – or halt execution manually when you suspect an error by choosing Break

Page 33: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 33CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

3. Use debugging tools and techniques to isolate bugs and to monitor code flow:

– Set breakpoints to halt execution at certain points to look for problems such as

incorrect variable types, mixups in variable names, flaws in logical comparisons, endless loops, garbled output, problems with arrays, and so on.

Page 34: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 34CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

– Add a watch expression at design time or while in break mode to allow you to monitor the value of a particular expression as your code runs.

– Single step or procedure step through your code to trace through the application as it's running.

– Use the Debug window to test individual lines of code or procedures, or to change values.

– Enter break mode and choose Calls from the Debug window to see where your code is currently executing and trace the path showing how it got there.

Page 35: Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters

Lec6 P 35CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton

4. Try out bug fixes and then make edits:

– Test individual lines of new or debugged code in the Debug window.

– Search and replace code for all occurrences of an error, checking other procedures, forms, or modules with related code.