Monday, November 7, 2011

Debugging tips in Visual Studio

1)      Break on all Exceptions (even if it is handled) Go to Debug  > Exception (Ctrl Alt E). This will be useful when you are given an unknown application that collects all the errors and shows it at once on the top of the screen. You do not know the source of the error (or the exception). Just enable this option and find out directly.
2)       A faster way of debugging an error is to do it backwards i.e. go the known bottommost function and start doing a step out (Shift – F11) to climb up and see where it fails. This is better (faster) than debugging from start (top) and doing Next >> Next. The “starting from bottommost” technique will give all the call flows at once in the CallStack window.
3)      If you want to change value of any variable while debugging (during runtime), use immediate window and just write VAR1=”abc” in the window.
Or hover over the variable , write the value there itself and press Enter.
4)       Conditional BreakPoint in Loops: You can attach a breakpoint in a FOR or FOREACH loop and when it hits it, right-click on the breakpoint and you can put a CONDITION say when p.FirstName=="Paul". So you dont have to iterate through the  whole collection one by one. There are other options as well when you right click.
5)      To attach a breakpoint at every place for a particular function (called Function Breakpoint), Go to the breakpoints window à New à Break at function (or press Ctrl + B). Type the function name.

No comments:

Post a Comment