C.3. The Locals and Watch Windows

In the preceding section, you learned that the Quick Info feature allows you to examine the value of a variable. In this section, you will learn how to use the Locals window to assign new values to variables while your program is running. You will also use the Watch window to examine the values of expressions.

  1. Inserting breakpoints. Set a breakpoint at line 23 (Fig. C.9) in the source code by left clicking in the margin indicator bar to the left of line 23. Use the same technique to set breakpoints at lines 26 and 28 as well.

    Figure C.9. Setting breakpoints at lines 23 and 26.

  2. Starting debugging. Select Debug > Start Debugging. Type 49.99 at the Enter deposit amount for account1: prompt (Fig. C.10) and press Enter so that the program reads the value you just entered. The program executes until the breakpoint at line 23.

    Figure C.10. Entering the deposit amount before the breakpoint is reached.

  3. Suspending program execution. When the program reaches line 23, Visual Studio suspends program execution and switches the program into break mode (Fig. C.11). At this point, the statement in line 20 (Fig. C.2) has input the depositAmount that you entered (49.99), the statement in lines 2122 has output that the program is adding that amount to the account1 balance and the statement in line 23 is the next statement that will execute.

    Figure C.11. Program execution pauses when debugger reaches the breakpoint at line 23.

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

  4. Examining data. Once the program has entered break mode, you can explore the values of your local variables using the debugger's Locals window. To view the Locals window, select Debug > Windows > Locals. Click the plus box to the left of account1 in the Name column of the Locals window (Fig. C.12). This allows you to view each of account1's instance variable values individually, including the value for balance (50). Note that the Locals window displays properties of a class as data, which is why you see both the Balance property and the balance instance variable in the Locals window. In addition, the current value of local variable depositAmount (49.99) and the args parameter of Main are also displayed.

    Figure C.12. Examining variable depositAmount.

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

  5. Evaluating arithmetic and boolean expressions. You can evaluate arithmetic and boolean expressions using the Watch window. Select Debug > Windows > Watch to display the window (Fig. C.13). In the first row of the Name column (which should be blank initially), type (depositAmount + 10) * 5, then press Enter. The value 299.95 is displayed (Fig. C.13). In the next row of the Name column in the Watch window, type depositAmount == 200, then press Enter. This expression determines whether the value contained in depositAmount is 200. Expressions containing the == symbol are bool expressions. The value returned is false (Fig. C.13), because depositAmount does not currently contain the value 200.

    Figure C.13. Examining the values of expressions.

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

  6. Resuming execution. Select Debug > Continue to resume execution. Line 23 executes, crediting the account with the deposit amount, and the program enters break mode again at line 26. Select Debug > Windows > Locals. The updated balance instance variable and Balance property value are now displayed (Fig. C.14).

    Figure C.14. Displaying the value of local variables.

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

  7. Modifying values. Based on the value input by the user (49.99), the account balance output by the program should be $99.99. However, you can use the Locals window to change variable values during program execution. This can be valuable for experimenting with different values and for locating logic errors in programs. In the Locals window, click the Value field in the balance row to select the value 99.99. Type 66.99, then press Enter. The debugger changes the value of balance (and the Balance property as well), then displays its new value in red (Fig. C.15). Now select Debug > Continue to execute lines 2627. Notice that the new value of balance is displayed in the Command Prompt window.

    Figure C.15. Modifying the value of a variable.

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

  8. Stopping the debugging session. Select Debug > Stop Debugging. Delete all breakpoints.

C 4 Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands

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