Sunday, April 14, 2013

Tips & Tricks : Visual Studio Code Flow Navigation Without Code Map II - Code Definition Window

This is a continuation of previous post. Here we are discussing code flow navigation options in Visual Studio. You can start with the previous post but that is not a prerequisite for this post. In this post we are going to discuss a great productivity tool available in Visual Studio. This tool is Code Definition Window. The tool was released with Visual Studio 2005 but I haven't seen many developers taking advantage of this tool for their every day code analysis and development. On the contrary, I have seen developers using Go to Definition context menu from the code editor which results in a stack of windows which not only makes it hell to manage the opened files but it also slows down the performance of your Visual Studio instance as the load is increased.

What is this used for?
Code Definition Window tool window is provided to look up the definition of the symbol based on where the cursor is positioned. This provides a read-only view of symbol definition. If this is a type then it opens up the definition of the type. If the symbol is a member within the same or a parent class then it opens the definition of symbol and scroll the view to the definition of the symbol.

Code Definition Window is integrated with code editor window for C# and VB. It is also integrated with Class View explorer.

Opening the Code Definition Window
Code Definition Window option is available in View Menu. You can also use Ctrl + D key combination to open the window.



Cursor Not Positioned on a Symbol Yet?
If you open the window when cursor hasn't been positioned on a symbol yet then it shows empty window with the following message:



How to Use It?
Mostly, this is one of the tool window that should always be opened during development and debugging sessions. I have mostly found it useful when kept in a separate monitor. While we are using a type or a method, it can be used to glimpse the actual definition including details about the method parameters and comments. You can also look at other details about the class before using that in your code.

Definition of Type
If your cursor is positioned on a type name then it opens the file containing the type definition in Code Definition Window. The following is the display of the code definition window as soon as I have positioned the cursor on an interface type, named ICustomerModel in the editor. The definition of the interface type is opened in the Code Definition window.



Definition of Symbol
Code Definition window can also be used to look up the definition of members of the same type. Like we are using an instance member in a method, it is really helpful to see the declaration of the member in the class. Code Definition window is even better than this. It shows the definition of the member even if it is defined in a parent class. Isn't that really cool? In the following code, we are looking at the definition of CustomerModel type, as soon as we have placed the cursor on RaisePropertyChanged method in CustomerID setter, the definition defined in the base class (ModelBase) shows up in the Code Definition window.



Type Definition from Class View
Code Definition Window is also integrated with Class View. As we select a type in Class View, the code definition of the type appears in the code definition window. Following is the view when we have selected CustomerModel type in the class view while keeping the Code Definition window opened. See how it is updated with the selected type definition.



It also supports the members of a type when selected in the bottom pane in Class View. The following is the view when we have selected a member Address from CustomerModel and the Code Definition Window view is updated and scrolled to the definition of Address in CustomerModel.



Opening Code Editor
If you find some code in Code definition window that needs some editing then just right click anywhere in the code definition window and select Edit Definition. Visual Studio opens the code file in code editor. Here we were looking at the definition of class, called Program, and we found some code that needs editing. Let's select Edit Definition and update it as follows:

No comments: