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

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Visual Studio 2010 and Controls.
Advertisements

Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
Programming Types of Testing.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Example 2.
Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
How to Debug VB .NET Code.
Visual Basic Debugging Tools Appendix D 6/27/20151Dr. Monther Aldwairi.
Understanding the Mainline Logical Flow Through a Program (continued)
JavaScript, Fourth Edition
Lec2 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 2 Back to Index v Basic Data Types v Arithmetic.
Lec4 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 4 Back To Index v Iteration - Looping v Arrays.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
1 Chapter 9 Writing, Testing, and Debugging Access Applications.
XP Chapter 7 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Enhancing User Interaction Through Programming Chapter.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Why to Create a Procedure
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Using Arrays and File Handling
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
VB – Debugging Tools Appendix D. Why do we need debugging? Every program has errors, and the process of finding these errors is debugging Types of errors.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
JavaScript, Fourth Edition
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Chapter 2 Introduction to Visual Basic Programming Visual Basic.NET.
5.01 Understand Different Types of Programming Errors
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
Controlling Program Flow with Decision Structures.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
AVCE ICT – Unit 7 - Programming Session 12 - Debugging.
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.
COMPUTER PROGRAMMING I SUMMER Understand Different Types of Programming Errors.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Harvard Mark I Howard Aiken was a pioneer in computing and a creator of conceptual design for IBM in the 1940s. He envisioned an electro-mechanical computing.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Programming in visual basic .net Visual Basic Building Blocks
ME 142 Engineering Computation I
Testing and Debugging.
Microsoft Access Illustrated
Using Procedures and Exception Handling
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Variables and Arithmetic Operations
Tonga Institute of Higher Education
CS285 Introduction - Visual Basic
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

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

Lec6 P 2 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Modularity of Code: Procedures v 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

Lec6 P 3 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton v 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; } v The function return the result via the parameter fresult

Lec6 P 4 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Question v Modify the example to show code to return the result via the functions name - ie. No output paraments to the function. Answer v Students to add the answer

Lec6 P 5 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton v 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

Lec6 P 6 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Passing Parameters by Value & Reference v 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.

Lec6 P 7 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Parameter Declarations v As in C you can declare parameters are either input (Value) or output (Variable/address of/by reference) v Value parameters are declared locally to the Procedure, hence used for input to a procedure. v Reference parameters share the same address as arguments and are used to return values from procedures (output). v Input : Value Students to add details v Output : Reference Students to add details

Lec6 P 8 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Modularity of Code: Sub-Procedures v Calling: Call subname(value1, value2, etc) or subname value1, value2, etc v Sub-Procedure Structure: Sub subname(value1 As type, Value2 As type, etc) declarations (local) statements End Sub Arguments Parameters

Lec6 P 9 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Modularity of Code: Functions v Passes back a value to the calling procedure v Calling – variable = funcname( value1, value2, etc) v Function structure: – Function funcname( value1 As type, value2 As type, etc) As type statements funcname = returnvalue End Function Note the return type

Lec6 P 10 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Sub-Procedure Locations v Where sub-procedures are located:

Lec6 P 11 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Adding a Sub-Procedure or Function -1 v 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 v Then select New Procedure... from the sub-menu

Lec6 P 12 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton v This will bring up a dialog: v You should select the radio button for either a Sub-Procedure or a Function v You then need to enter the Sub-Procedure’s name

Lec6 P 13 CP2030 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 Sub or Function CubeNo () End Function v You should then add any parameters required and a return type if it is a function

Lec6 P 14 CP2030 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 Long CubeNo = lNum * lNum * lNum End Function

Lec6 P 15 CP2030 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 Sub or Function CubeNo () End Function v You should then add any parameters required and a return type if it is a function

Lec6 P 16 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Example v Let’s consider a simple program that works out the VAT that is payable on goods v You enter the price, click on Calc and the VAT and total price are calculated

Lec6 P 17 CP2030 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 *.175 End Sub  Calc Command Button: Sub Command1_Click () Dim cPrice As Currency Dim cVat As Currency cPrice = Val(Text1.Text) Call CalcVAT(cPrice, cVat) Label1.Caption = cVat Label2.Caption = cPrice + cVat End Sub Poor use of Parameters

Lec6 P 18 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Solution2 : Functions v We can declare CalcVAT as a function instead of a sub-procedure  Function CalcVAT: Function CalcVAT(cValue As Currency) As Currency CalcVAT = cValue *.175 End Function  Calc Command Button: Sub Command1_Click () Dim cPrice As Currency Dim cVat As Currency cPrice = Val(Text1.Text) cVat = CalcVAT(cPrice) Label1.Caption = cVat Label2.Caption = cPrice + cVat End Sub Again Poor use of parameters

Lec6 P 19 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Question v 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.

Lec6 P 20 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Passing Arrays v 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

Lec6 P 21 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Calling Procedures & Passing Arrays v 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

Lec6 P 22 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Control Arrays v Visual Basic provides us with arrays of standard data types and user defined types : Dim inum(20) As Integer Dim studrec(100) As Studenttype v 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

Lec6 P 23 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Control Arrays 1 v Control Array is a number of controls that have the same event handler, the system passes the controls index into the procedure

Lec6 P 24 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Control Arrays 2 v The Control Array event handler can use a select statement instead of an if statement

Lec6 P 25 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Control Arrays: Creating v 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

Lec6 P 26 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton  Creating Control Arrays at run-time: Load ControlName(Index%) Unload ControlName(Index%) v Students to add notes on this section

Lec6 P 27 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Demo 1 - control arrays v Create a form with two text fields, a label to hold an answer and a control array of four command buttons. v When a button is pressed the operation & answer should be displayed in Label1: = -7 or = 4

Lec6 P 28 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Select Case Statement & Control Arrays v 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

Lec6 P 29 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Demo 2 - Labels as a control array Create array of Labels at design time

Lec6 P 30 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Debugging - Introduction v There are three types of errors : v 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

Lec6 P 31 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Source Of errors v 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. v Logical errors the program doesn't perform as intended, and produces incorrect results.

Lec6 P 32 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton How to debug - overview v Debugging is a process by which you find and resolve errors in your code. v 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

Lec6 P 33 CP2030 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 u incorrect variable types, u mixups in variable names, u flaws in logical comparisons, u endless loops, u garbled output, u problems with arrays, and so on.

Lec6 P 34 CP2030 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.

Lec6 P 35 CP2030 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.