Software Development

Take advantage of code snippets in Visual Studio 2005

Visual Studio 2005's auto-complete feature is new for C# developers and offers additional functionality for Visual Basic and other languages. Discover how using Visual Studio 2005's code snippets can help you boost productivity.

The auto-complete features of Visual Studio .NET are wonderful if you are a Visual Basic developer, but C# developers sorely missed out on the feature. (Of course, C# developers who never use Visual Basic may be unaware of its existence.) Fortunately, Visual Studio 2005 brings auto-complete for code snippets to C#, J#, and XML, as well as additional features for Visual Basic. Let's take a closer look at auto-complete.

Code snippets

Code snippets are reusable, task-oriented blocks of code. It can be as simple as creating an exception to sending an e-mail. They are combined with IntelliSense to increase developer productivity, making them available with a few keystrokes. While you can easily create your own snippets, the Visual Studio 2005 installation includes a set of Visual Basic and Visual C# code snippets. One popular code snippet allows you to easily add a property to class. A property is added by simply typing prop into your Visual Studio source and hitting [Tab] twice. The result of this action follows:

private int myVar;
public int MyProperty
{
get { return myVar;}
set { myVar = value;}
}
Weekly .NET tips in your inbox
TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET.
Automatically sign up today!

Visual Basic doesn't automatically display a context menu—you simply begin typing an element and then selecting [Tab] to insert a snippet. The following Visual Basic code was generated after typing Property and hitting [Tab]:

Private newPropertyValue As Integer
Public Property NewProperty() As Integer
Get
Return newPropertyValue
End Get
Set(ByVal value As Integer)
newPropertyValue = value
End Set
End Property

The generated code is generic, so you can easily customize it for your application. Visual Studio 2005 simplifies the process with the variable type and names appearing in boxes (which are green by default) so you can enter your values and quickly tab through to change.

Snippets are available for many code elements; they commonly use language keywords in the IntelliSense pop-up window. All you need to do is select the keyword and hit [Tab] twice to insert the snippet. Some C# examples include if, for, foreach, switch, while, and using. The for loop code snippet follows:

for (int i = 0; i < length; i++)
{

}

Or, we can generate the following code for the Visual Basic For statement:

For index As Integer = 1 To 10

Next

Typing and hitting [Tab] is one approach to using snippets, but you can also right-click your mouse and select the Insert Snippet option from the context menu (or Surround With in C#).

Code snippet design


Code snippets are built with XML; they are contained in their own files using the .snippet file extension. The XML markup in the .snippet file provides snippet metadata, which includes a description, shortcut, title, and the actual code. In addition, Visual Basic code snippets may include code references.

You can get a better idea of code snippet file layout by examining the snippets installed with Visual Studio. By default, they are stored in the following folder:



C:\Program Files\Microsoft Visual Studio 8\LANGUAGE\Snippets\1033\

The LANGUAGE portion of the path is replaced by the language being used like VB, VC#, and XML. Look at the C# prop code snippet to get a better idea of how they are coded. The prop code snippet is in the following directory:

C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#

Listing A contains the code snippet file. The CodeSnippets element is the root, and it may contain one or more CodeSnippet elements. Its metadata is defined in the Header element. The name that appears in the IntelliSense pop-up window is defined by the Shortcut element. The Declarations element is optional—it defines replaceable sections of the snippet. The Code element specifies the code that is actually inserted when the snippet is used. Visual Studio includes the Code Snippets Manager to manage snippets.

Code Snippets Manager

The Code Snippets Manager allows you to set the directories and individual snippets that you want available to insert into your code. It is available via the Visual Studio Tools menu, and provides the following information and options:

  • Language: A drop-down list that allows you to select the development language whose code snippet folders are displayed in the folder list.
  • Location: This displays the path to the folders in the folder list or to the selected code snippet file.
  • Folder list window: This displays the set of subfolders and code snippet files for the language selected.
  • Description window: This displays information about the folder or code snippet file selected in the folder list. For a code snippet file, the text from its Author, Description, Shortcut, and Type fields is displayed.
  • Add: This button allows you to add a folder to the list. It opens the code snippet directory window to navigate to the folder.
  • Remove: This button allows you to remove a folder from the list, but it does not physically delete it.
  • Import: This button allows you to import snippet files to a specific folder.
  • Search Online: This opens a search page that allows you to search for snippets online.

The creation and management of code snippets can be an involved process. Thankfully, the Visual Studio IDE provides the tools to streamline the process.

Get a productivity boost

IntelliSense is a great feature that speeds up development by assisting with code development and negating the need to look up code syntax and so forth. Code snippets take it a step further by extending syntax help by inserting code blocks. This is nothing new to Visual Basic developers, but it's a welcome sight for their C# counterparts. Visual Studio 2005 includes a complete code snippet architecture to aid their use. Next week we extend this concept by showing you how to create your own code snippet, which you can reuse in your application.

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

Miss a column?

Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

0 comments