Visual Studio Debugging Techniques – Part2

Posted: October 12, 2014 in Debugging
Tags: , ,

12thOct_1

As a developer, regardless of your programming language or the platform that you target, we use the debugger on a daily basis. If you are a developer using Microsoft technologies then you need to learn and understand how to make the most of the Visual Studio debugger so that you can be more productive and effective in your everyday development.

Irrespective of your experience level, I encourage you to read my blog post series on Visual Studio debugging techniques. This is my second blog post in this series and I am sure that by the time you finish reading this entire series, you will have some good knowledge of debugger features which you would want to use immediately when you get back to your computer!

  
  Step Into Tips

12thOct_3

 

 

 

I am sure you might have used ‘Step Into’ (F11) feature of Debug tool bar whenever you would like to debug the code related to a function call line by line. If the line contains a function call, Step Into executes only the call itself, then halts at the first line of code inside the function. Otherwise, Step Into executes the next statement.

12thOct_4

12thOct_QTTNow I will tell you another interesting usage of F11. Whenever we would like to debug an application we setup breakpoint(s) and hit F5. But instead of starting the debugging by pressing F5 if you press F11 it would takes you out of the designer mode into debug mode to the first line of your code that is going to be executed.

 

12thOct_QTTThis is a useful, whenever you would like to debug startup issues in your project. Imagine how you would have done it otherwise? You could have scanned the code and identified what was the first line that is going to be executed outside designer and setup a break point before hitting F5 and remove the break point later. The key here is it saves your time.

 

12thOct_QTTAnother useful tip is that you can start debugging by selecting a line in the source code and use Run to Cursor (CTRL+F10) option instead of using Start Debugging F5. How cool is that? Even if you would like to debug the click event of a button then directly go to the source code and use Run to Cursor (CTRL+F10) option.

 Import and Export Data Tips

At times we want to continue the debugging session which we are doing on a coworker machine or perhaps you would like to handover the debug session to someone else for their quick feedback.

The first step towards accomplishing this activity is to use import/export breakpoint functionality. But what if you would like to write some debugging comments in your code which could be helpful to those who are resuming this debugging session.

Drag-Drop Pin Data Tip

To accomplish this you need to Pin the Data Tip. Data tip is kind of an advanced tool tip message which is used to inspect the objects or variable during the debugging of the application. When debugger hits the breakpoint, if you mouse over to any of the objects or variables, you can see their current values.

 12thOct_5

12thOct_QTT

 

 

Hitting Ctrl turns the data tip currently displayed nearly transparent. It becomes visible again as soon as it is released.

Change Value Using Data Tips

DataTips is also used to change the value while debugging. This means it can be used like a watch window. From the list of Pinned objects, you can change their value to see the impact on the program.

 12thOct_6

 

 

 

We can Pin/Unpin Object/Variable Inspect or Data Tip. By pressing the pin Icon Data Tip can be pinned or unpinned.

 12thOct_7

 

 

 

Click on expand to see comments downwards arrow and type your comments here. BTW it is a multi-line comment.

12thOct_8

12thOct_9

Last Session Debugging Value

This is another great feature of Visual Studio 2010 debugging. If you pinned some data tip during the debugging, the value of pinned item will remain stored in a session.

It means even when you stop debugging this information is still available in Visual studio.

12thOct_QTT

We can go back to the same line and hover the mouse on the pin to see the details of the last debugging session value as shown in the below picture:

 

 

 12thOct_10

Export/Import/Clear Data Tips

Later you can use Debug > Export Data Tips command to export the data tips and Debug > Import Data Tips command to import it on to a different machine and continue debugging. Debug > Clear Data Tips can be used to clear the data tips in the solution.

12thOct_11

Debugger Display Attribute

You can notice some specific information displayed for a type when I hover the mouse on it.

12thOct_12

 

 

In the above example when I hover the mouse on SystemInfo type, debugger is displaying some specific information related to this type. It is because we have decorated SystemInfo class as shown below.

12thOct_13

 

 

 

 

 

 

 

Debugger display attributes allow the developer of the type, who specifies and best understands the runtime behavior of that type, to also specify what that type will look like when it is displayed in a debugger.

The DebuggerDisplayAttribute constructor has a single argument: a string to be displayed in the value column for instances of the type. This string can contain braces ({ and }). The text within a pair of braces is evaluated as an expression. For example, the following C# code causes “Count = 4” to be displayed when the plus sign (+) is selected to expand the debugger display for an instance of MyHashtable.

12thOct_14

Data Tips from comments

Did you know that you can get tool tips from comments? I can hover mouse on comment which actually shows the data tip. Which means you can leave comments with useful expressions.

12thOct_15

 

 

 

12thOct_QTTYou can do this on the fly as well. Which means during debugging you can add expressions as comments and determine the value. How cool is that?

 

 

12thOct_16

 

 

12thOct_17

 

I will catchup with you next week with my last blog post on this series with some more interesting tips.

Until then Happy Debugging!!!

Advertisements

Comments are closed.