Sometimes you will need to execute a program line-by-line to find and fix logic errors. Stepping through a portion of your program this way can help you verify that a method's code executes correctly. The commands you learn in this section allow you to execute a method line-by-line, execute all the statements of a method or execute only the remaining statements of a method (if you have already executed some statements within the method).
1. |
Setting a breakpoint. Set a breakpoint at line 23 by left clicking in the margin indicator bar (Fig. C.16). Figure C.16. Setting a breakpoint in the program. |
2. |
Starting the debugger. Select Debug > Start Debugging. Enter the value 49.99 at the Enter deposit amount for account1: prompt. Program execution halts when the program reaches the breakpoint at line 23. |
3. |
Using the Step Into command. The Step Into command executes the next statement in the program (the yellow highlighted line of Fig. C.17) and immediately halts. If the statement to execute is a method call, control transfers to the called method. The Step Into command allows you to follow execution into a method and confirm its execution by individually executing each statement inside the method. Select Debug > Step Into (or press F11) to enter the Credit method (Fig. C.18). Figure C.17. Using the Step Into command to execute a statement. Figure C.18. Stepping into the Credit method. |
4. |
Using the Step Over command. Select Debug > Step Over to enter the Credit method's body (line 17 in Fig. C.18) and transfer control to line 18 (Fig. C.19). The Step Over command behaves like the Step Into command when the next statement to execute does not contain a method call or access a property. You will see how the Step Over command differs from the Step Into command in Step 10. Figure C.19. Stepping over a statement in the Credit method. (This item is displayed on page 1451 in the print version) |
5. |
Using the Step Out command. Select Debug > Step Out to execute the remaining statements in the method and return control to the calling method. Often, in lengthy methods, you will want to look at a few key lines of code, then continue debugging the caller's code. The Step Out command is useful for executing the remainder of a method and returning to the caller. |
6. |
Setting a breakpoint. Set a breakpoint (Fig. C.20) at line 28 of Fig. C.2. You will make use of this breakpoint in the next step. Figure C.20. Setting a second breakpoint in the program. (This item is displayed on page 1451 in the print version) |
7. |
Using the Continue command. Select Debug > Continue to execute until the next breakpoint is reached at line 28. This feature saves time when you do not want to step line-by-line through many lines of code to reach the next breakpoint. |
8. |
Stopping the debugger. Select Debug > Stop Debugging to end the debugging session. |
9. |
Starting the debugger. Before we can demonstrate the next debugger feature, you must restart the debugger. Start it, as you did in Step 2, and enter the same value (49.99). The debugger pauses execution at line 23. |
10. |
Using the Step Over command. Select Debug > Step Over (Fig. C.21). Recall that this command behaves like the Step Into command when the next statement to execute does not contain a method call. If the next statement to execute contains a method call, the called method executes in its entirety (without pausing execution at any statement inside the methodunless there is a breakpoint in the method), and the arrow advances to the next executable line (after the method call) in the current method. In this case, the debugger executes line 23, located in Main (Fig. C.2). Line 23 calls the Credit method. Then, the debugger pauses execution at line 26, the next executable statement. Figure C.21. Using the debugger's Step Over command. (This item is displayed on page 1452 in the print version) |
11. |
Stopping the debugger. Select Debug > Stop Debugging. Remove all remaining breakpoints. |
Preface
Index
Introduction to Computers, the Internet and Visual C#
Introduction to the Visual C# 2005 Express Edition IDE
Introduction to C# Applications
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Exception Handling
Graphical User Interface Concepts: Part 1
Graphical User Interface Concepts: Part 2
Multithreading
Strings, Characters and Regular Expressions
Graphics and Multimedia
Files and Streams
Extensible Markup Language (XML)
Database, SQL and ADO.NET
ASP.NET 2.0, Web Forms and Web Controls
Web Services
Networking: Streams-Based Sockets and Datagrams
Searching and Sorting
Data Structures
Generics
Collections
Appendix A. Operator Precedence Chart
Appendix B. Number Systems
Appendix C. Using the Visual Studio 2005 Debugger
Appendix D. ASCII Character Set
Appendix E. Unicode®
Appendix F. Introduction to XHTML: Part 1
Appendix G. Introduction to XHTML: Part 2
Appendix H. HTML/XHTML Special Characters
Appendix I. HTML/XHTML Colors
Appendix J. ATM Case Study Code
Appendix K. UML 2: Additional Diagram Types
Appendix L. Simple Types
Index