C.2. Breakpoints and the Continue Command

We begin by investigating breakpoints, which are markers that can be set at any executable line of code. When a running program reaches a breakpoint, execution pauses, allowing you to examine the values of variables to help determine whether logic errors exist. For example, you can examine the value of a variable that stores the result of a calculation to determine whether the calculation was performed correctly. You can also examine the value of an expression.

To illustrate the features of the debugger, we use the program in Figs. C.1 and C.2, which creates and manipulates an object of class Account (Fig. C.1). This example is similar to an example you saw in Chapter 4 (Figs. 4.154.16). Therefore, it does not use features we present in later chapters like += and if...else. Execution begins in Main (lines 829 of Fig. C.2). Line 10 creates an Account object with an initial balance of $50.00. Account's constructor (lines 1013 of Fig. C.1) accepts one argument, which specifies the Account's initial balance. Lines 1314 of Fig. C.2 output the initial account balance using Account property Balance. Line 16 declares and initializes local variable depositAmount. Lines 1920 prompt the user for and input the depositAmount. Line 23 adds the deposit to the Account's balance using its Credit method. Finally, lines 2627 display the new balance.

Figure C.1. Account class with a constructor to initialize instance variable balance.

 1 // Fig. C.01: Account.cs
 2 // Account class with a constructor to
 3 // initialize instance variable balance.
 4
 5 public class Account
 6 {
 7 private decimal balance; // instance variable that stores the balance
 8
 9 // constructor
10 public Account( decimal initialBalance )
11 {
12 Balance = initialBalance; // set balance using property Balance
13 } // end Account constructor
14
15 // credit (add) an amount to the account
16 public void Credit( decimal amount )
17 {
18 Balance = Balance + amount; // add amount to Balance
19 } // end method Credit
20
21 // a property to get and set the account balance
22 public decimal Balance
23 {
24 get
25 {
26 return balance;
27 } // end get
28 set
29 {
30 // validate that value is greater than 0;
31 // if it is not, balance is set to the default value 0
32 if ( value > 0 )
33 balance = value;
34
35 if ( value <= 0 )
36 balance = 0;
37 } // end set
38 } // end property Balance
39 } // end class Account

Figure C.2. Creating and manipulating an Account object.

 1 // Fig. C.02: AccountTest.cs
 2 // Create and manipulate an Account object.
 3 using System;
 4
 5 public class AccountTest
 6 {
 7 // Main method begins execution of C# application
 8 public static void Main( string[] args )
 9 {
10 Account account1 = new Account( 50.00M ); // create Account object
11 12 // display initial balance of each object using property Balance 13 Console.Write( "account1 balance: {0:C} ", 14 account1.Balance ); // display Balance property 15 16 decimal depositAmount; // deposit amount read from user 17 18 // prompt and obtain user input 19 Console.Write( "Enter deposit amount for account1: " ); 20 depositAmount = Convert.ToDecimal( Console.ReadLine() ); 21 Console.Write( "adding {0:C} to account1 balance ", 22 depositAmount ); 23 account1.Credit( depositAmount ); // add to account1 balance 24 25 // display balance 26 Console.Write( "account1 balance: {0:C} ", 27 account1.Balance ); 28 Console.WriteLine(); 29 } // end Main 30 } // end class AccountTest
account1 balance: $50.00

Enter deposit amount for account1: 49.99
adding $49.99 to account1 balance

account1 balance: $99.99

In the following steps, you will use breakpoints and various debugger commands to examine the value of the variable depositAmount (declared in Fig. C.2) while the program executes.

   

1.

Inserting breakpoints in Visual Studio. First, ensure that AccountTest.cs is open in the IDE's code editor. To insert a breakpoint, left click inside the margin indicator bar (the gray margin at the left of the code window in Fig. C.3) next to the line of code at which you wish to break, or right click that line of code and select Breakpoint > Insert Breakpoint. You can set as many breakpoints as you like. Set breakpoints at lines 19, 23 and 28 of your code. A solid circle appears in the margin indicator bar where you clicked and the entire code statement is highlighted, indicating that breakpoints have been set (Fig. C.3). When the program runs, the debugger suspends execution at any line that contains a breakpoint. The program then enters break mode. Breakpoints can be set before running a program, in break mode and during execution.
 

Figure C.3. Setting breakpoints.

(This item is displayed on page 1444 in the print version)

 
   

2.

Beginning the debugging process. After setting breakpoints in the code editor, select Build > Build Solution to compile the program, then select Debug > Start Debugging (or press the F5 key) to begin the debugging process. While debugging a console application, the Command Prompt window appears (Fig. C.4), allowing program interaction (input and output).
 

 

Figure C.4. Account program running.

 

3.

Examining program execution. Program execution pauses at the first breakpoint (line 19), and the IDE becomes the active window (Fig. C.5). The yellow arrow to the left of line 19 indicates that this line contains the next statement to execute. The IDE also highlights the line as well.
 

Figure C.5. Program execution suspended at the first breakpoint.

 
 

4.

Using the Continue command to resume execution. To resume execution, select Debug > Continue (or press the F5 key). The Continue command will execute the statements from the current point in the program to the next breakpoint or the end of Main, whichever comes first. The program continues executing and pauses for input at line 20. Enter 49.99 in the Command Prompt window as the deposit amount. When you press Enter, the program executes until it stops at the next breakpoint (line 23). Notice that when you place the mouse pointer over the variable name depositAmount, its value is displayed in a Quick Info box (Fig. C.6). As you'll see, this can help you spot logic errors in your programs.
 

Figure C.6. QuickInfo box displays value of variable depositAmount.

 

5.

Continuing program execution. Use the Debug > Continue command to execute line 23. The program displays the result of its calculation (Fig. C.7).
 

Figure C.7. Program output.

 
   

6.

Disabling a breakpoint. To disable a breakpoint, right click a line of code in which the breakpoint has been set and select Breakpoint > Disable Breakpoint. You can also right click the breakpoint itself and select Disable Breakpoint. The disabled breakpoint is indicated by a hollow circle (Fig. C.8)the breakpoint can be re-enabled by clicking inside the hollow circle, or by right clicking the line marked by the hollow circle (or the circle itself) and selecting Breakpoint > Enable Breakpoint.
 

Figure C.8. Disabled breakpoint.

(This item is displayed on page 1446 in the print version)

 

7.

Removing a breakpoint. To remove a breakpoint that you no longer need, right click the line of code on which the breakpoint has been set and select Breakpoint > Delete Breakpoint. You also can remove a breakpoint by clicking the circle in the margin indicator bar.
 

8.

Finishing program execution. Select Debug > Continue to execute the program to completion.
 

C 3 The Locals and Watch Windows

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



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Similar book on Amazon

    Flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net