(How)
C^# You Are - 2 September 2007
In continuation from last week the VS2005 IDE is the topic this week. For most the Visual Studio 2005 IDE is
the gateway to .NET development. If you don't know your way around it then your productivity will suffer. A word of warning
about the features discussed here. They may not be available in all editions.
Ths is highly likely for the Express or Standad edition of VS2005.
- You are developing an application. You copy a couple of folders to the project
directory. How can you easily add the folders and all the files to the project? Answer
The easiest solution is to click the Show All Files button at the
top of Solution Explorer. The new folders will appear in a disabled state.
Right click the folders and select Include in Project to add the
folder and its children to the project.
This is ideal in most cases because Solution Explorer will not allow you to create
a folder in the project if the folder already exists on disk. Therefore in
order to add an existing folder with files to a project you would have to resort
to renaming folders and copying files around.
- What purpose does the Unload Project menu option in Solution Explorer
serve? Answer
This option allows to you temporarily exclude a project from the solution.
Excluding a project prevents it from being built or deployed when the solution is
built or deployed. Excluding projects is often a better way to temporarily
remove a project from a solution than simply deleting it. Deleting a project
removes the project dependencies from the solution. This is ideal for a project
that will eventually be added to the solution but, for now, is not ready and therefore
should not be used.
- Why are the HACK, TODO and UNDONE comments special to the IDE? Answer
When used in code these comments are automatically added to the Task List for the
document. Whenever the document is open the comments are extracted from the
code and placed into the Task List. This provides a poor man's task list which
is great for documenting areas of the code that should be revisited. Clicking
on the item in the Task List will jump to the appropriate area of the code.
You can add your own comments as well. For example you might add a TEST element
(through Tools\Options -> Task List) that identifies area of code that needs
further testing.
- How can you view the types and member of an assembly you reference in your code? Answer
Of course Intellisense is a great tool. The problem is that it only shows
you types and members as you type. If you want to get a general overview of
what types and members are available in an assembly then there are several different
views available to you.
Object Browser (OB) is the first view that you should consider. It appears
as a document window and will show you all (or filtered) members of an assembly
including its namespaces, types and members. You can filter the list to show
only those members that you can actually call. When you select a member it
displays the same information you'd expect to see from Intellisense. It is
hyperlinked so clicking on a type name will take you to the type's definition.
The biggest issue with OB is that it is displayed as a document window so if you
are trying to view the documentation while using it in another document then it
can get difficult (see the next question for a solution).
The next view is the Class View (CV). This is a tool window. CV provides
a hierarchial view of the types and members in the project (and optionally the references).
Unlike OB, CV does not actually display any documentation for the members.
Instead when you double click on an item it will jump to the item's definition in
your code. If you double click an item contained in a reference then it will
open OB to the appropriate location instead.
The third view is the Code Definition Window (CDW). I believe this window
is only available in the "high-end" editions or perhaps with the VS2005 SDK installed.
This is another document window. While of limited use for your own code it
does provide a code-style view of any type you point it to. As you select
type names in a document window the CDW refreshes with the skeleton of the code
as it would appear if you wrote it yourself, including documentation. The
implementation details are left out. Not generally useful over the other views
but still interesting to play around with.
- You want to compare two documents in VS2005. How can you easily compare the
documents without flipping between the two? Answer
Text panes are the answer here. They are surprisingly hidden for something
so useful. You can create both horizontal or vertical panes. To create
a new pane (the default is to show one) click and drag one of the document tabs.
When you release the tab while inside the document window you will get prompted
to create a horizontal or vertical pane. As you would expect a horizontal
pane basically divides the document window into two panes, one on top of the other.
Each has its own set of tabs. You can drag and drop tabs between the panes.
For comparing or copying documents this is the way to go. Vertical panes work
in the same way exept they divide the window in the middle so you have a left and
right pane.
The discussion earlier about the OB limitation can be overcome by using panes.
Note that you can create multiple panes but you can not mix horizontal and vertical
panes at this time.
- You have some non-source code files you would like to store with the solution.
How can you do this? Answer
VS2005 supports the concept of a solution folder. A solution folder is nothing
more than a logical folder where you can place files. You can actually store
any file you want under any folder in the project but it is generally a good idea
to keep only source files under project folders. The solution folder is a
good place to store solution wide files such as scripts for building the solution,
FxCop project files, documentation project files, versioning files, etc. To
create a solution folder simply right click on the solution in Solution Explorer
and select Add -> New Solution Folder.
I recommend against storing documentation files such as design documents and project
plans in VS2005. Most PMs do not have VS2005 so store only those files directly
needed or related to the source code in VS2005.
A side note. By default the solution is not shown in Solution Explorer.
To show it you must go to Tools\Options -> Projects and Solutions -> Always
show solution.
- You create a bunch of temporary projects in VS2005 in order to test things.
You are getting tired of clearing out your MRU for the test projects. How
can you make this easier? Answer
The easiest solution is to not do it to begin with. Normally VS2005 will automatically
save a solution when you add it in VS2005. This is an option though.
There is really no benefit in auto-saving a solution when you initially create it.
As soon as you close VS2005 or try to open a new solution you're going to get asked.
To prevent VS2005 from auto-saving a solution when it is created go to Tools\Options
-> Projects and Solutions -> Save new projects when created and uncheck it.
Now VS2005 will create the solution in a temporary location. You can add files,
edit the code, add references and even compile and run the code without saving the
file. You can not add more than one project to the solution without saving.
When you try to close the solution you are prompted to either save the solution
or discard it. If you discard the solution then all the code is deleted from
disk. Even better the MRU is not updated with this non-existent solution.
FYI the temporary solution is stored at %USERPROFILE%\AppData\local\Temporary Project
(for Vista) and %USERPROFILE%\Local Settings\Application Data\Temporary Project
(for XP). Note that not all projects work with this option. Specifically
web sites do not.
- How can you rename a namespace across all files in the solution? Answer
You can try to select the namespace name, right-click and select Refactor\Rename
but you'll get an error. Don't fret. It was, IMHO, an oversight by MS.
To rename a namespace (any part of it) select the name only (not the entire namespace),
right-click and select Refactor\Rename to rename it. The downside is that
if you want to rename a namespace from A.B.C to D.E.F then you'll have to do the
rename once for each namspace part but, alas, nothing is every easy.
- You want to show the property window docked to the left while editing in WinForms
designer but hide it while editing code. How can you do it? Answer
Unfortunately there is no automatic way. VS2005 distinguishes between only
edit and debug modes. In this case you want to differentiate between designer
and code modes. What you need to do is open the property window and dock it
the left side whenever you activate a designer file. When you deactivate the
designer file you want to hide the property window again. Tedious but simple.
Whenever you find yourself reiterating the same steps over and over again you should
consider using a macro. If you can perform some task through VS2005's menus
or keyboard then you can probably do it through a macro. You can create a
macro that automatically opens the property window and docks it to the left when
invoked. Another macro will hide the property window if it is exposed.
Once you've create the two macros you can associate them to hotkeys through Tools\Options
-> Environment\Keyboard.
Well, get started. What you don't know how to write macros? Macros are
accessible through the Macro Explorer (Alt+F8 or View\Other Windows\Macro Explorer).
You can create a new macro through here. Macros are written in VB code.
Still, you don't have to write the code. VS2005 ships with quite a few macros
for you to use. A set of these is LoadView/SaveView. Configure VS2005
the way you want it to appear when opening a designer file. Invoke the SaveView
macro (using Macro Explorer). You can now use the LoadView macro to load the
view later on. Associate the LoadView macro with a hotkey to allow you to
switch to the view whenever you want. You can extend this to automatically
switch to (and away) from the view when the designer file is activated. Consider
it an exercise for the user.
- You find yourself continually adding the same comments to each file. A header
for all files. A function header for each function. A type header for
each type. How can you make this easier? Answer
The general solution is to use snippets. You can create your own code snippets.
With a snippet you can auto-generate code as needed. Snippets can be invoked
either through the context menu (Insert Snippet or Surround With) or you can associate
the snippet with a shortcut defined by the snippet. Unfortunaely snippets
are somewhat limited beyond simply inserting blocks of text. Still, you can
generate comment headers easily with snippets.
Creating a new snippet is not hard once you get over the XML syntax used.
Rather than going over the details please refer to MSDN for information. I
recommend that you use an existing snippet as a template. There also isn't
an easy way within VS2005 to create or edit snippets. Fortunately MS has released
a utility called the Code Snippet Editor to allow you to create, edit and test snippets.
It says it is for VB but it works for any snippet language. Download it at
http://msdn2.microsoft.com/en-us/vbasic/ms789085.aspx.
I would be remiss if I didn't point out that there are third-party tools that can
solve the initial problem as well. GhostDoc is a popular tool for auto-generating
comments for code elements. It is accessible from http://www.roland-weigelt.de/ghostdoc/.