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;} 
}

Visual Basic doesn't automatically display a context menu — you simply begin typing an element and then select [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 customise 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; snippets 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, you 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 mark-up 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#\Visual C#

The code below 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.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>prop</Title>
<Shortcut>prop</Shortcut>

<Description>Code snippet for property and backing field</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>

<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>

</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>

<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>

<Code Language="csharp"><![CDATA[private $type$ $field$;
public $type$ $property$
{
get { return $field$;}
set { $field$ = value;}
}
$end$]]>
</Code>

</Snippet>
</CodeSnippet>
</CodeSnippets>

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.

Advertisement

Cast your .NET This was published in Cast your .NET, check every Thursday for more stories

Related links

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

Log in


Sign up | Forgot your password?

  • Staff Facebook's portal for the masses

    This week, Facebook took a number of strategic steps toward its goal of giving people the "power to share and make the world more open and connected." That's how founder and CEO Mark Zuckerberg described the mission statement for Facebook. Read more »

    -- posted by Staff

  • Brendon Chase Do you trust data in the cloud?

    Cheap hosted storage, app engines, and hosted code libraries. Can you really trust your data, or your client's data in the magical Web 2.0 cloud? Read more »

    -- posted by Brendon Chase

  • Staff The future remains yesterday

    Remember when MySQL was blazingly fast and cared little for SQL standards? When MySQL regarded a view as something nice from your window and a trigger was treated as a weaponry component? Those days are set to return with a MySQL fork called Drizzle. Read more »

    -- posted by Staff

What's on?