390 likes | 492 Views
Tutorial 13 – Enhancing the Wage Calculator Application Introducing Function Procedures and Sub Procedures. Outline 13.1 Test-Driving the Enhanced Wage Calculator Application 13.2 Classes and Procedures 13.3 Function Procedures
E N D
Tutorial 13 – Enhancing the Wage Calculator ApplicationIntroducing Function Procedures and Sub Procedures Outline 13.1 Test-Driving the Enhanced Wage Calculator Application 13.2 Classes and Procedures 13.3FunctionProcedures 13.4 Using Sub Procedures in the Wage Calculator Application 13.5 Using the Debugger: Debug Toolbar 13.6 Wrap-Up
Objectives • In this tutorial, you will learn to: • Construct applications modularly from pieces called procedures. • Work with “built-in” procedures. • Distinguish between Function procedures and Sub procedures, and determine when each should be used. • Create your own Function procedures and Sub procedures.
13.1 Test-Driving the Enhanced Wage Calculator Application Figure 13.1 WageCalculator executing. • Load the Wage Calculator application • Debug > Start • Set Hourly wage:TextBox to 10 • Set Weekly hours:TextBox to 45 • Click CalculateButton
13.2 Classes and Procedures • Classes • Programmer defined or predefined • Reusing code • Procedures • Function procedures • Sub procedures
Procedure Description Example Math.Max(x, y) Math.Max( 2.3 , 12.7 ) 12.7 Returns the larger value is x y of and Math.Max( - 2.3 , - 12.7 ) - 2.3 is Math.Min(x, y) Math.Min( 2.3 , 12.7 ) 2.3 Returns the smaller value of is x and y Math.Min( - 2.3 , - 12.7 ) - 12.7 is Ma th.Sqrt(x) x Math.Sqrt( 9 ) 3.0 Returns the square root of is Math.Sqrt( 2 ) 1.4142135623731 is Pmt(x, y, z) Pmt( 0.05 , 12 , - 4000 ) Calculates loan payments where is x y 451.301640083261 specifies the interest rate, specifies the number of payment z periods and specifies the principal value of the l oan Val(x) x Val( “5” ) 5 Returns a numeric value for is Val( “5a8” ) 5 is Val( “a5” ) 0 is .Format(x, y) String String String .Form at( “{0:C}” , 1.23 ) Returns values where is x y “$1.23” is a format string, and is a value to be formatted as a String 13.2 Classes and Procedures Figure 13.2 Some predefined Visual Basic .NET procedures.
13.3FunctionProcedures Figure 13.3 Hypotenuse Calculator GUI.
13.3 FunctionProcedures • View code for HypotenuseCalculator • Notice the six variables • Three are side lengths • Three are side lengths squared • If…Then…ElseStatement • Checks validity • Calculates hypotenuse
Lengths for sides A, B, and hypotenuse Square of lengths for sides A, B, and hypotenuse Message dialog displays if negative values (or zero) are entered 13.3 FunctionProcedures Figure 13.4Hypotenuse Calculator template code.
13.3FunctionProcedures • Procedure definition • Procedure header • Begins with Function • Followed by procedure name and variable declaration • Parameter list • Variables used only within function procedure • Return type • Procedure Body • End Function
Function procedure header End Functionkeywords mark the end of aFunctionprocedure 13.3FunctionProcedures Figure 13.5FunctionprocedureSquare.
Calculating squares by using the ^ operator 13.3FunctionProcedures Figure 13.6 Square procedure definition
13.3FunctionProcedures • Invoking a procedure • Procedure call • Procedure name followed by arguments inside parentheses • Example: Square(dblSideA) • Parameter Info window • When the procedure is complete, the caller, or calling procedure, regains control.
Calling procedure Square 13.3FunctionProcedures Figure 13.7Invoking procedure Square.
Parameter Info window 13.3FunctionProcedures Figure 13.8 Parameter Info window.
13.3FunctionProcedures Figure 13.9 Completing the btnCalculate_Click event handler.
13.3FunctionProcedures Figure 13.10 Hypotenuse Calculator application executing. • Load the Hypotenuse Calculator application • Debug > Start • Set Length of Side A:TextBox to 3 • Set Length of Side B:TextBox to 4 • Click the Calculate HypotenuseButton
TextBoxes used to input three values 13.3FunctionProcedures Figure 13.11 Maximum application in design view.
Calling a procedure that has not yet been defined is an error 13.3FunctionProcedures Figure 13.12Invoking Function procedure Maximum.
Empty Functionprocedure Maximum 13.3FunctionProcedures Figure 13.13 MaximumFunction procedure.
Calling Math.Max to determine the maximum of two values 13.3FunctionProcedures Figure 13.14 Math.Max returns the larger of its two arguments.
13.3FunctionProcedures Figure 13.15 Maximum application executing.
13.4 Using Sub Procedures in the Wage Calculator Application • Sub procedures • Definition is same as a Function procedure, but the keyword Sub replaces Function • No return value or return type
Call to DisplayPay 13.4 Using Sub Procedures in the Wage Calculator Application Figure 13.16 btnCalculate_Click calls DisplayPay.
DisplayPay calculates and displays the user’s gross earnings 13.4 Using Sub Procedures in the Wage Calculator Application Figure 13.17 Sub procedure DisplayPay definition.
13.4 Using Sub Procedures in the Wage Calculator Application Figure 13.18 Function procedure CheckOverTime definition.
Call to procedure CheckOvertime 13.4 Using Sub Procedures in the Wage Calculator Application Figure 13.19 DisplayPay calls Function procedure CheckOvertime.
Step into Step out Continue Stop debugging Step over 13.5 Using the Debugger: Debug Toolbar Figure 13.21 Debug Toolbar. • Debug toolbar • Controls the debugging process • View > Toolbars > Debug
Debug toolbar Breakpoint set on a line containing a procedure call 13.5 Using the Debugger: Debug Toolbar Figure 13.22 Setting a breakpoint. • Set breakpoints by clicking in the margin indicator bar
Next statement to execute is a procedure call 13.5 Using the Debugger: Debug Toolbar Figure 13.23 Statement calls procedure DisplayPay.
13.5 Using the Debugger: Debug Toolbar • Step Into • Executes next statement in application • If next statement is procedure call, statements will be executed step by step • Step Out • Returns control to calling procedure • Used only after a procedure has been stepped into • Step Over • Executes next statement in application • If next statement is procedure call, procedure will be fully executed
Control is transferred to the procedure definition 13.5 Using the Debugger: Debug Toolbar Figure 13.24 Using the debug toolbar’s Step Into Button.
Procedure CheckOverTime will be executed without stepping into it when the Step Over Button is clicked 13.5 Using the Debugger: Debug Toolbar Figure 13.25 Using the debug toolbar’s Step Over Button.
13.5 Using the Debugger: Debug Toolbar Figure 13.25 Using the debug toolbar’s Step Over Button again.
13.5 Using the Debugger: Debug Toolbar • Continue • Continues executing code • Does not stop until next break point or end of event handler • Stop • Ends debugging session
13.5 Using the Debugger: Debug Toolbar Figure 13.25 Using the debug toolbar’s Continue Button.