March 20, 2011

C# - Debugging C-Sharp Applications - Part 2 - Various Debugging Windows

1. Introduction

In this second part, we will still use the same sample that we used in the previous part. Here, we will cover some other important windows that help the process of debugging.

You can read the series from the C# Label.

Part 1: Debugging Basic
Part 2: Debugging Windows [You are here]
Part 3: Trace Logging

The screen shot of the application is given below (Yes! I am using the Same sample from the previous part of debugging):

2. Call Stack

The call stack window helps to see the order in which function call occurred from the application startup to the place where you break the application. Before I explain more about it please follow the steps to bring the "Call Stack window".

1) Open the Attached application and put a breakpoint in the function IsPrime as shown below.

2) Run the Sample and enter the input 24 then press the button Get Prime Sum. This will get you to the break point.
3) Now bring the Call Stack Window by navigating Debug=>Window=>Call Stack. The call stack window is shown below.

In the above call stack window, the first line in the top shows that the application is paused in the function IsPrime by taking the input parameter theNumber with the value 3. Now, look at the second line. The function SumIfPrime receives the parameter theNumber with value 24 from the function btnGetResult_Click (Event handler right?) and it called the function IsPrime where we kept the break point to temporarily halt the application. You can also note the beginning of the application that is; program entry point Main().

You can easily move to the function shown in the call stack by double clicking the particular line. It will be useful when you are working on a big project where function call, as shown above, may happen between dll or from a different assembly.

3. Auto and Local Window

Before I explain about these windows, we will add a function so that you can know the difference between them. The function added to the form is shown below:

//006: Auto Locals
void AutoLocals()
    int a, b, c, d, e;
    a = 10;
    b = 20;
    c = 30;
    d = 40;
    e = 50;

And this function is called in the constructor:

//001 : Declarations
int PrimeSum = 0;

public frmDebug()

Now put a breakpoint on the function in the statement b=20. Run the sample with the input number 24. Once the breakpoint is hit, open the "Auto and Local Windows". You can reach these windows by navigating Debug=>Windows. If it is not available to bring it from the tools=>customize option. It is explained in the first article to bring the step-in, step-out, step-over and the link is provided in the introduction section. The auto window is shown below:

 The breakpoint is at b=20, and it displays the variable that changed or involved in the previous statement plus the variable that involved in the execution of the current statement. Also, the changed variable value is shown in red color, which is not recognized in the first statement as I highlighted it (Oh! Man, my mistake). Now, step over the statement to move the control to the next statement say C=30 and see how the Autos window changes.

The local window shows the variable, which is local to the execution scope. Here, in our case, if the execution point is at d=40 for example, all the variables are displayed as they are local between the curly braces start and end. Below is screen shot of the locals window:

4. Output Window

The output window is useful to display the content written by the code. We should use System.diagnostic namespace to access the debug utility method. Write the following piece of code in the function SumIfPrime as shown below:

//004: When a passed in number is prime, sum it.
void SumIfPrime(int theNumber)
    for (int i = 1; i <= theNumber; i++)
        if (IsPrime(i) == true)
            txtOutput.AppendText(i.ToString() + ", ");
            PrimeSum = PrimeSum + i;
         System.Diagnostics.Debug.WriteLine( string.Format("Prime Detected {0}", i));

Now, the above code will write the string specified in the output window. Just, image how do you write something on your console output window. It is similar to that but what you write is displayed on the output window. Remove all the breakpoints and run the sample with the input 24 again and look at the output window. Before you click the button on the form, right-click the output window and select clear all from the context menu. This will help you to see what you wrote in a clean way. If output window is not displayed bring it from Debug=>Windows or from the customize option. The output window content after running the application is shown below:

5. Immediate window

When output window allows you to print something on it through the utility function supplied by the dot net framework, the immediate window allows examining the execute expressions. Put a break point in the statement e=50 in the autolocals function. Run the sample attached with the input value 24. Once the breakpoint hit, bring the output window.

Now write the following in the immediate window and hit enter: ?c
The above command retrieves the value of c and displays it on the next line. Looks simple and you are wondering what is there in it? I can simply hover my mouse on the variable c to see what value it has. To know the real use, type the following and hit enter and I think justification on why the immediate window is useful not required. I used it often when framing a complex string manipulation statements. It is painful to run the application again and again to make perfect string manipulation expressions. Use it if you are new to it and you will become addicted to this window.

?(a*a) + (2*a*b) + (b*b)

The below screen shot shows immediate window in action:

Note: The sample is created in VS2005. If you have the latest IDE, Say yes to the conversion UI displayed.

Source Code : DownLoad

No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Friend :)