Presentation is loading. Please wait.

Presentation is loading. Please wait.

13 Enhancing the Wage Calculator App

Similar presentations


Presentation on theme: "13 Enhancing the Wage Calculator App"— Presentation transcript:

1 13 Enhancing the Wage Calculator App
Introducing Function Procedures and Sub Procedures © by Pearson Education, Inc. All Rights Reserved.

2 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

3 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

4 Introduction The best way to develop and maintain a large app is to construct it from smaller, more manageable pieces. This technique is known as divide and conquer. Manageable pieces include program components—known as procedures. © by Pearson Education, Inc. All Rights Reserved.

5 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

6 Test-Driving the Enhanced Wage Calculator App
Run the completed app (Fig. 13.1). Click the Calculate Button. The result is displayed in the Gross earnings: Label. © by Pearson Education, Inc. All Rights Reserved.

7 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

8 13.2 Classes and Procedures
The key to creating large apps is to break them into smaller pieces. In object-oriented programming, these pieces consist primarily of classes, which can be further broken down into methods. Programmers combine programmer-defined classes and methods with preexisting code in the .NET Framework Class Library. Using preexisting code saves time, effort and money. The concept of reusing code increases efficiency for app developers. © by Pearson Education, Inc. All Rights Reserved.

9 13.2 Classes and Procedures (Cont.)
Figure 13.2 lists several pre-existing Visual Basic methods. © by Pearson Education, Inc. All Rights Reserved.

10 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

11 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

12 Creating the Hypotenuse Calculator App
Open HypotenuseCalculator.sln in the HypotenuseCalculator directory (Fig. 13.3). © by Pearson Education, Inc. All Rights Reserved.

13 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

14 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

15 Creating the Hypotenuse Calculator App (Cont.)
Switch to Code view, and examine the code provided in the template, shown in Fig Initially, lines 8–11 are underlined by the IDE because they are not yet used. We’ve provided an incomplete event handler for the Calculate Hypotenuse Button. © by Pearson Education, Inc. All Rights Reserved.

16 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

17 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

18 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

19 Creating the Hypotenuse Calculator App (Cont.)
Add lines 26–28 of Fig after event handler calculateButton_Click and before the End Class keywords, then press Enter. The keywords End Function are added by the IDE (line 30) when you press Enter. © by Pearson Education, Inc. All Rights Reserved.

20 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

21 Creating the Hypotenuse Calculator App (Cont.)
The procedure name can be any valid identifier and is followed by a set of parentheses containing the parameter declarations, if any. The declaration in the parentheses is known as the parameter list, where variables (called parameters) are declared. Parameters enable a procedure to receive data that helps it perform its task. The parameter list can contain zero or more declarations separated by commas. © by Pearson Education, Inc. All Rights Reserved.

22 Creating the Hypotenuse Calculator App (Cont.)
A Function procedure returns one value after it performs its task. To specify the return type, the parameter list is followed by the keyword As, which is in turn followed by a data type. The type that follows As, known as the return type, indicates the type of the result returned from the Function. The Function procedure ends with the keywords End Function. © by Pearson Education, Inc. All Rights Reserved.

23 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

24 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

25 Creating the Hypotenuse Calculator App (Cont.)
The ^ operator (Fig. 13.6) is used to calculate the square of input. A Return statement is used to return this value. © by Pearson Education, Inc. All Rights Reserved.

26 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

27 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

28 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

29 Creating the Hypotenuse Calculator App (Cont.)
These lines call Square by using the procedure name followed by a set of parentheses that contain the procedure’s argument (Fig. 13.7). © by Pearson Education, Inc. All Rights Reserved.

30 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

31 Creating the Hypotenuse Calculator App (Cont.)
When you typed the opening parenthesis after the procedure name, you probably noticed that the Visual Basic IDE displays a window containing the procedure’s argument names and types (Fig. 13.8). This is the Parameter Info feature of the IDE. © by Pearson Education, Inc. All Rights Reserved.

32 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

33 Creating the Hypotenuse Calculator App (Cont.)
Line 32 (Fig. 13.9) calls the .NET Framework Class Library method Sqrt of class Math (by using the dot operator). © by Pearson Education, Inc. All Rights Reserved.

34 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

35 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

36 Creating the Hypotenuse Calculator Application (Cont.)
Run and test the app (Fig. 13.10). © by Pearson Education, Inc. All Rights Reserved.

37 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

38 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

39 Creating a Function Procedure That Returns the Largest of Three Numbers
Open Maximum.sln in the Maximum directory (Fig. 13.11) and switch to Design view. © by Pearson Education, Inc. All Rights Reserved.

40 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

41 Creating a Function Procedure That Returns the Largest of Three Numbers (Cont.)
Double click the Maximum Button to create an event handler. The IDE underlines Maximum in blue on your screen—indicating a compilation error—because Function procedure Maximum has not yet been defined (Fig. 13.12). © by Pearson Education, Inc. All Rights Reserved.

42 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

43 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

44 Creating a Function Procedure That Returns the Largest of Three Numbers (Cont.)
Create the Function procedure Maximum (Fig. 13.13). © by Pearson Education, Inc. All Rights Reserved.

45 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

46 Creating a Function Procedure That Returns the Largest of Three Numbers (Cont.)
The maximum is determined by using the Max method of .NET Framework Class Library class Math (Fig. 13.14). The Return statement terminates the procedure’s execution and returns the result (the value of finalMaximum) to the calling procedure. © by Pearson Education, Inc. All Rights Reserved.

47 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

48 Creating a Function Procedure That Returns the Largest of Three Numbers (Cont.)
Run and test the app (Fig. 13.15). © by Pearson Education, Inc. All Rights Reserved.

49 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

50 Creating a Sub Procedure within the Wage Calculator App
Open WageCalculator2.sln in the WageCalculator2 directory. Double click the Calculate Button to generate an event handler (Fig. 13.16). © by Pearson Education, Inc. All Rights Reserved.

51 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

52 Creating a Sub Procedure within the Wage Calculator App (Cont.)
Add Sub procedure DisplayPay to your app (lines 18–39 of Fig. 13.17). There’s no return type, because Sub procedures do not return values. When control reaches the End Sub statement, control returns to the calling procedure. © by Pearson Education, Inc. All Rights Reserved.

53 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

54 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

55 Creating a Function Procedure within the Wage Calculator App
Note that the return type of the procedure is Boolean (Fig. 13.18)—the value returned by the procedure must be a Boolean. © by Pearson Education, Inc. All Rights Reserved.

56 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

57 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

58 Creating a Function Procedure within the Wage Calculator Application (Cont.)
In Sub procedure DisplayPay, replace the statement (line 26 of Fig. 13.17) If hours <= HOUR_LIMIT Then with line 26 of Fig © by Pearson Education, Inc. All Rights Reserved.

59 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

60 Figure 13.20 presents the source code of the app.
Outline Figure 13.20 presents the source code of the app. © by Pearson Education, Inc. All Rights Reserved.

61 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

62 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

63 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

64 13.5 Using the Debugger: Debugging Controls
These ToolStripButtons (Fig. 13.21) provide convenient access to commands in the Debug menu. © by Pearson Education, Inc. All Rights Reserved.

65 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

66 Using the Debugger: Debugging Controls
In the Wage Calculator app, set a breakpoint in line 15 (Fig. 13.22). Select Debug > Start Debugging. Enter the value 7.50 in the Hourly wage: TextBox, and enter 35 in the Weekly hours: TextBox. Click the Calculate Button © by Pearson Education, Inc. All Rights Reserved.

67 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

68 Using the Debugger: Debugging Controls (Cont.)
The Step Into ToolStripButton ( ) executes the app’s next statement (Fig. 13.23). If the next statement to execute is a procedure call and you click the Step Into ToolStripButton, control transfers to the called procedure so you can execute the procedure’s statements and confirm that they execute properly. © by Pearson Education, Inc. All Rights Reserved.

69 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

70 Using the Debugger: Debugging Controls (Cont.)
Click the Step Into ToolStripButton to enter procedure DisplayPay (Fig. 13.24). © by Pearson Education, Inc. All Rights Reserved.

71 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

72 Using the Debugger: Debugging Controls (Cont.)
Click the Step Over ToolStripButton ( ) to execute the current statement without stepping into it (Fig. 13.25). © by Pearson Education, Inc. All Rights Reserved.

73 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

74 Using the Debugger: Debugging Controls (Cont.)
Click the Step Over ToolStripButton again. Step Over behaves like the Step Into when the next statement to execute does not contain a procedure call. If the next statement to execute contains a procedure call, the called procedure executes in its entirety, and the yellow arrow advances to the next executable line (Fig. 13.26). © by Pearson Education, Inc. All Rights Reserved.

75 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

76 Using the Debugger: Debugging Controls (Cont.)
Set a breakpoint at the end of procedure DisplayPay in line 39 (Fig. 13.27). © by Pearson Education, Inc. All Rights Reserved.

77 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

78 Using the Debugger: Debugging Controls (Cont.)
Clicking the Continue ToolStripButton ( ) executes any statements between the next executable statement and the next breakpoint or simply continues program execution if there are no more breakpoints. Click the Stop Debugging ToolStripButton ( ) to end the debugging session and return the IDE to design mode. © by Pearson Education, Inc. All Rights Reserved.

79 13.6 Optional Parameters When a procedure is invoked repeatedly with the same argument value, you can specify that such a parameter is an Optional parameter. When the argument for an Optional parameter is omitted, the compiler rewrites the procedure call, inserting the default value. There are three rules for using Optional parameters: Each Optional parameter must have a default value. The default value must be a constant expression. All parameters after an Optional parameter must also be Optional parameters. © by Pearson Education, Inc. All Rights Reserved.

80 13.6 Optional Parameters (Cont.)
Consider the Function BoxVolume: Function BoxVolume(Optional length As Integer = 1, Optional width As Integer = 1, Optional height As Integer = 1 ) As Integer Return length * width * height End Function ' BoxVolume Each parameter has a default value specified with an = and a literal value (1). © by Pearson Education, Inc. All Rights Reserved.

81 13.6 Optional Parameters (Cont.)
You can now invoke Function BoxVolume several different ways: BoxVolume() ' returns 1; default values used for length, width, height BoxVolume(10) ' returns 10; default values used for width, height BoxVolume(10, 20) ' returns 200; default value used for height BoxVolume(10, 20, 30) ' returns 6000; no default values used BoxVolume(, 20, 30) ' returns 600; default value used for length BoxVolume(10, , 30) ' returns 300; default value used for width Comma placeholders are used when an omitted argument is not the last argument in the call. © by Pearson Education, Inc. All Rights Reserved.


Download ppt "13 Enhancing the Wage Calculator App"

Similar presentations


Ads by Google