Using Visual C Debugging Tools


Using Visual C# Debugging Tools

Visual C# includes a number of debugging tools to help you track down and eliminate bugs. In this section, you'll learn to use the primary tools of break points, the Immediate window, and the Error List windowthree tools that form the foundation of any debugging arsenal. There are more tools, such as the Watch window and Locals window, and as you progress into more detailed development, you should explore these tools.

Working with Break Points

Just as an exception halts the execution of a procedure, you can deliberately stop execution at any statement of code by creating a break point. When Visual C# encounters a break point while executing code, execution is halted at the break statement before it is executed. Break points enable you to query or change the value of variables at a specific instance in time, and they let you step through code execution one line at a time.

You're going to create a break point to help troubleshoot the exception in your lngAnswer = statement.

Adding a break point is simple. Just click in the gray area to the left of the statement at which you want to break code execution (you could have also placed the cursor on the statement and pressed F9 to toggle the breakpoint on and off). When you do so, Visual C# displays a red circle, denoting a break point at that statement (see Figure 15.4). To clear the break point, you would click the red circle (but don't do this now).

Figure 15.4. Break points give you control over code execution.


By the Way

Break points are saved with the project. You don't have to reset all your break points each time you open the project.


Click the gray area to the left of the lnAnswer = statement to create a break point as shown in Figure 15.4. After you've set the break point, press F5 to run the program. Click the Perform Division button again. When Visual C# encounters the break point, code execution is haltedright before the statement with the break point executes, and the procedure with the break point is shown. In addition, the cursor is conveniently placed at the statement with the current break point. Notice the yellow arrow overlaying the red circle of the break point (see Figure 15.5). This yellow arrow marks the next statement to be executed. It just so happens that the statement has a break point, so the yellow arrow appears over the red circle (the yellow arrow won't always be over a red circle, but it will always appear in the gray area aligned with the next statement that will execute).

Figure 15.5. A yellow arrow denotes the next statement to be executed.


When code execution is halted at a break point, you can do a number of things. See Table 15.1 for a list of the most common actions. For now, press F5 to continue program execution. Again, you get a Format Exception.

Table 15.1. Actions That Can Be Taken at a Break Point

Action

Keystroke

Description

Continue Code Execution

F5

Continues execution at the current break statement.

Step Into

F11

Executes the statement at the break point and then stops at the next statement. If the current statement is a function call, F11 enters the function and stops at the first statement in the function.

Step Over

F10

Executes the statement at the break point and then stops at the next statement. If the current statement is a function call, the function is run in its entirety; execution stops at the statement following the function call.

Step Out

Shift+F11

Runs all the statements in the current procedure and halts execution at the statement following the one that called the current procedure.

Stop Debugging

Shift+F5

Stops debugging the project and returns to design mode.


Using the Immediate Window

Break points themselves aren't usually sufficient to debug a procedure. In addition to break points, you'll often use the Immediate window to debug code. The Immediate window is a Visual Studio IDE window that generally appears only when your project is in Run mode. If the Immediate window isn't displayed, you can show it by opening the Debug menu, then choosing Windows > Immediate. Using the Immediate window, you can type in code statements that Visual C# executes immediately (hence the name). You'll use the Immediate window now to debug our problem statement example.

Type the following statement into the Immediate window and press Enter:

? txtInput.Text


Although it isn't intuitive, the ? character has been used in programming for many years as a shortcut for the word print. The statement you entered simply prints the contents of the Text property of the text box to the Immediate window.

Notice how the Immediate window displays the text "". This indicates that the text box is empty. The statement throwing the exception is attempting to use Convert.ToInt64() to convert the contents of the text box to a long (which is a 64 bit integer). The Convert.ToInt64() method expects data to be passed to it, yet the text box has no data (the Text property contains an empty string). Consequently, a Format exception occurs because Convert.ToInt64() doesn't know how to convert an empty string to a number.

You can do a number of things to prevent this error. The most obvious is to ensure that the text box contains a value before attempting to use Convert.ToInt64(). You're going to do this now.

Visual C# now supports on-the-fly code editingsomething that was sorely lacking in earlier versions of .NET. This means you can now modify code while debugging it, so that you no longer have to stop the project to make changes and then run it once more to test your changes.

Close the exception window and put the cursor between the declaration statement (long lngAnswer;) and the statement with the error and press Enter. Next, enter the following code statements:

if (txtInput.Text.Length == 0) return;


Now, remember the yellow arrow used to indicate the next statement that will execute? It indicates that if you continue code execution now, the statement that throws the exception will run once more. We want our new statements to execute. Follow these steps to designate the new code as the next code to be executed:

1.

Click on the yellow arrow and hold down the mouse button.

2.

Drag the yellow arrow to the new if statement.

3.

Release the mouse button.

Now, the yellow arrow should indicate that the next statement to execute will be the if statement (see Figure 15.6).

Figure 15.6. You can drag the yellow arrow to change which statement gets executed next.


Now, press F5 to continue running the project. This time, Visual C# won't throw an exception, and it won't halt execution at your break point because the test you just created caused code execution to leave the procedure before the statement with the break point was reached.

Next, follow these steps:

1.

Type your name into the text box and click the Perform Division button again. Now that the text box is no longer empty, execution passes the statement with the exit test and stops at the break point.

2.

Press F5 to continue executing the code, and again you'll receive an exception. This exception is once again a Format exception; the statement still doesn't like the format of the data being dealt with.

3.

Close the exception window (click the little x in the upper-right corner), and type the following into the Immediate window (be sure to press Enter when you're finished):

? txtInput.Text


The Immediate window prints your name.

Well, you eliminated the problem of not supplying any data to the Convert.ToInt64() method, but something else is wrong.

Press F5 to continue executing the code and take a closer look at the description of the exception. Toward the bottom of the Exception window is the text View Detail Click this link now, and Visual C# displays a View Detail window with more information about the exception (see Figure 15.7).

Figure 15.7. The View Detail window gives you important information for fixing exception problems.


The text on the View Detail window says {"Input string was not in a correct format."}. It apparently still doesn't like what's being passed to the Convert.ToInt64().

By now, it might have occurred to you that no logical way exists to convert alphanumeric text to a number; Convert.ToInt64() needs a number to work with. You can easily test this by following these steps:

1.

Click OK to close the View Detail window.

2.

Choose Stop Debugging from the Debug menu.

3.

Press F5 to run the project.

4.

Enter a number into the text box and click the button. Code execution again stops at the break point.

5.

Press F11 to execute the statement only. No errors this time! Press F5 to continue execution, and Visual C# displays the message box (finally). Click OK to dismiss the message box and then close the form to stop the project.

Did you Know?

You can use the Immediate window to change the value of a variable.


Because the Convert.ToInt64() expects a number, but the text box contains no intrinsic way to force numeric input, you have to accommodate for this situation in your code. You will learn how to deal with exceptions later on in this chapter using a catch statement.

Using the Output Window

The Output window (see Figure 15.8) is used by Visual C# to display various status messages and build errors. The most useful feature of the Output window, for general use, is the capability to send data to it from a running application. This is especially handy when debugging applications.

Figure 15.8. The Output window displays a lot of useful informationif you know what you're looking for.


You've already used the Output window in previous hours, but you might not have seriously considered its application as related to debugging. As you can see from Figure 15.8, some data sent to the Output window by Visual C# isn't that intuitivein fact, you can ignore much of what is automatically sent to the Output window. What you'll want to use the Output window for is printing data for debugging (as you have done and will do throughout this book). Therefore, it's no coincidence that printing to the Output window is accomplished via the Debug object.

To print data to the Output window, use the WriteLine() method of the Debug object, like this:

System.Diagnostics.Debug.WriteLine("Results = " + lngResults);


Whatever you place within the parentheses of the WriteLine() method is what is printed to the Output window. Note that you can print literal text and numbers, variables, or expressions. WriteLine() is most useful in cases where you want to know the value of a variable, but you don't want to halt code execution using a break point. For instance, suppose you have a number of statements that manipulate a variable. You can sprinkle WriteLine() statements into the code to print the variable's contents at strategic points. When you do this, you'll want to print some text along with the variable's value so that the output makes sense to you. For example:

System.Diagnostics.Debug.WriteLine       ("Results of area calculation = " + sngArea);


You can also use WriteLine() to create checkpoints in your code, like this:

System.Diagnostics.Debug.WriteLine("Passed Checkpoint 1"); // Execute statement here System.Diagnostics.Debug.WriteLine("Passed Checkpoint 2"); // Execute another statement here System.Diagnostics.Debug.WriteLine("Passed Checkpoint 3");


Many creative uses exist for the Output window. Just remember that the Output window isn't available to a compiled component; calls to the Debug object are ignored by the compiler when creating distributable components.




Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

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