ARTICLE

Some good Debugging tips

Posted by Amit Choudhary Articles | Visual Studio .NET March 14, 2011
Here I am going to explain for you a few shortcut keys which can make debugging faster and more accurate.
Reader Level:


Few Debugging things unleashed:

Being a software developer the debugging thing is the skill that used to debug the code and find the holes.

Visual Studio itself provides a good intellective debugging support. But a few shortcut keys can make debugging faster, more accurate and more helpful to find what's happening behind the scene.

QuickWatch or Watch - You want to see what's value of a variable or any objects properties or you want to change the value of a variable/property or any thing. Select the Object or the expression and right Click and click on Quickwatch or Watch.

Dbugg1.gif

You'll see the below window.
Dbugg2.gif
Now change the value field and click on Reevaluate or simply hit Enter key. The field value turns red, indicatng that your value is updated.

There's much more you can do with the Watch window. Go and explore it.

F5 - Here you start your debugging with your project. Or if you have place any break point and want to continue the execution.

F10 - If you want to see which lines of code are being executed. Then press place a break point somewhere in the code and when breakpoint hits press F10 to see the line by line execution.

F11 - Use it when your statement is a function call and you want to go inside the function to see what's happening. Press F11 to drill down the execution. Once the execution of the function is over the cursor will return back to the call statement.

Shift+F11 - Going forward with F11 thing, if you want to go back to the call statement immediately from the function body. Then press Shift+F11.

F12 - Just want to go to the definition or Declaration part of any Variable or Class or Function.

Tool Tip Window - Well this the common thing you see when you debug your application using Visual Studio. Just place your mouse cursor over the statement and it'll show you the values, properties or all runtime info as tooltip.

BreakPoint - when you want to pause the execution and see the run time things in your code. Just click on the left most bar and a Red ball will appear.
Lets talk about the BreakPoints:

Conditional Breakpoint: Suppose you have a loop and it's iterating for let's say 500 times. Then you want to check the value at particular time when the loop is iterated 100 times. Here 's the best way of achieving this. Right Click on the Red Dot

Dbugg3.gif

Dbugg4.gif

Here i is the looping variable. Now what's gonna happen the Break point will only hit when I have value 100. Also your Breakpoint dot will have a '+' sign and will look like this

Dbugg5.gif

There's more option available to change the behavior of a breakpoint Location, HitCount, Filter, WhenHit.. and EditLabels (To name your breakpoints) etc.
 
Just go and explore them what they do and how your breakpoint behavior changes.

More over you can control the Flow of execution by using the option SetNextStatement or by just moving the Yellow marked arrow to any statement you want to execute next. Just click on yellow arrow and drag and drop it on any statement.
Dbugg6.gif

Alternatively if you want to execute a particular statement by skipping a few line of code or want to go back to the previous statement to re-run their execution then just right click on the target statement and select "Set Next Statement"; see screen below.
Dbugg7.gif

Suppose you have opened many files at a time in the Visual Studio and you are debugging your application. And you have scrolled down in a file having 2000 lines of code and forget where my debugging cursor was, to go back to your next executing statement just right click any where and select "Show Next Statement".

Immediate Window: As the name indicates, if you want to execute your statement while debugging then open the Immediate window from Debug->Windows-> ImmediateWindow and write you statement in it and hit enter. It'll execute the statement and will show you the output.

Dbugg8.gif


Dbugg9.gif
Now in Debug->Windows you can see many options to see the runtime information.

CallStack - To see subsequent calls to the functions.

Modules - What modules are currently loaded in the process etc.

The last and very useful feature in visual studio while navigating in the code from here to there.
Navigation Backward and Navigation Forward - Now before I wrap up the last important thing you can use to quickly return to the last place. Visual Studio remembers your last place you were in your code. If you don't see the blue arrow icon as shown below then go to View-> Navigation Backward and Navigation Forward option.

Dbugg10.gif

Well I do enjoy with these features of Visual Studio while debugging. Hope who are new to Visual Studio will enjoy this article.

Happy Debugging :))

 

Login to add your contents and source code to this article
post comment
     

I am sorry; I was not clear. I mean that it is important to use the names (terms) that Microsoft uses so people know that when someone says "Step over" they mean the same thing that can be done using F10. If we say "single step" then we mean the same thing that can be done using F8 or F10 (and other keys). For F10 you say "to see which lines of code are being executed" and "to see the line by line execution". Pressing F10 does not show line by line execution; it shows execution of just one line. It will really help a lot if the article uses the terms Step Into, Step Over and Step Out the same as in the documentation.

Posted by Sam Hobbs Mar 15, 2011

Hi mahesh, Here's the story How i get inspired to write this? Long time ago i was searching on the same topic like Good Debugging tips and didn't get everything at one place. so i decided to do write it here. yet many things to explore on this topic :)) @Sam: Yes nice suggestion. Actually i do use these shortcut-keys and so mentioned the same :P Thanks guys for your feedback. Its keeps the spirit up.

Posted by Amit Choudhary Mar 15, 2011

Many times in the forums the only assistance we can suggest is to debug the program. This will help explain how to do that. It will be extremely useful therefore if the common name was used for things; such as "Step Over" for F10. For example see: http://msdn.microsoft.com/en-us/library/ff541507(VS.85).aspx

Posted by Sam Hobbs Mar 14, 2011

Nice to articles on the tools. We are lacking articles in this area. Thanks again!

Posted by Mahesh Chand Mar 14, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.