|
Code
Snippets in VS.NET 2005 - Part 1
During
application development, you might come across a situation where you need to
write the same code repeatedly with just a few changes. For example, you may
write code to fetch data from the database many times in your application. In
this case, you will open a text file, which has snippets of code, and you will
copy/paste from this file. VS.NET 2005 is coming up with a new feature called
code snippets, which will help developers to insert code snippets from a
snippet store into the program with just a few key strokes.
This
article describes how to use code snippets feature in Visual Studio .NET 2005
(VS.NET) and second part of this article explains how to create your own code
snippets. .
Introduction
A
code snippet is a fragment of code or text that you can store and reuse many
times. Using code snippets feature, you can store frequently used code in a
particular folder and you can reuse this code wherever you want with just a few
key strokes. In addition to being able to use a selection of pre-defined code
snippets, VS.NET 2005 allows you to create new snippets, delete unused
snippets, and modify existing snippets. In addition, you can create and
distribute your own code snippet files,
Code Snippets in VB.NET
Microsoft is planning to provide more than 500 code snippets for VB.NET.
According to this beta release, they have provided around 300 code snippets.
These code snippets have been divided into the following categories:
-
Application
-
Collections
-
Connectivity
-
Database
-
Data Types
-
File System
-
Language
-
Math
-
OS
-
Security
-
Windows Forms
These snippets are available at C:\Program Files\Microsoft Visual Studio
8\Vb\Snippets\1033. Note: In this release, these snippets are available at the
above-mentioned path, but in the final release, this path might vary. You can
search for “SnippetIndex.xml” file in your machine to find out the code
snippets folder for VB. To insert these snippets in code editor in VS.NET 2005,
you can use any of the following methods
-
Using context sensitive right click and then choosing insert snippet option.
-
Using snippet's registered shortcut (as defined by a snippet's XML file) and
then pressing tab.
-
You can also drag the XML code files from Windows Explorer onto your source
code file.
For example, place the cursor in the scope of method body and
then press right click. In this menu option, choose insert snippet. You will
get a list of code snippets as shown in the following figure.

In the list of code snippets, if you select “Call a
Parameterized Stored Procedure” code snippet; then the code for calling a
parameterized stored procedure will be inserted in code editor like this,
The inserted code will have one or more replacement points
highlighted in the code. Default color for editable expansion fields is yellow.
You can change the color by modifying the code snippet field setting in the
Fonts and Colors pane of the Options
dialog box in VS.NET. These yellow highlights represent tokens that you will
need to edit to finalize the expansion. Using the Tab key, you can navigate
between each highlighted item and edit accordingly.
You may or may not choose to change each replacement point. If
you move your mouse pointer over the replacement point, a ToolTip appears;
which explains how you need to change the code. The Code Editor recognizes a
snippet as a unit separate from your surrounding code until you close the
source-code file. As such, the highlighted replacement points remain in view
until you have closed the source file. Similarly if you change a replacement
point in the code, it will reflect in other places where you are using the same
replacement point. In the above example, if you change the connection property
dcNorthwind
to
oConn, this change will reflect
in three places, hence, improving your productivity.
Structure of Code Snippet
If you look at the code snippet file, it is just an xml file.
The file is stored with .vbsnippet extension.
Code snippet file for function declaration is shown below,
<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets>
<CodeSnippet format="1.0.0">
<Header>
<Title>Declare a Method</Title>
<Author>Microsoft Corporation</Author>
<Description>Add Function method declarations.</Description>
<HelpUrl /><Shortcut /><Keywords />
</Header>
<Snippet>
<References>
<Reference>
<Assembly>System.dll</Assembly> <Url />
</Reference>
</References>
<Imports>
<Import>
<Namespace>System</Namespace>
</Import>
</Imports>
<Declarations>
<Literal>
<ID>ReturnType</ID>
<Type>String</Type>
<ToolTip>Replace with the return value.</ToolTip>
</Literal>
<Literal>
<ID>ReturnValue</ID>
<Type>Integer</Type>
<ToolTip>Replace with the return value.</ToolTip>
</Literal>
</Declarations>
<Code Language="VB" Kind="method decl" Format="XML">
Public Function FunctionName() As <Literal ID="ReturnValue">Int</Literal>
' Add code here to evaluate function.
Return <Literal ID="ReturnValue">1</Literal>
End Function
</Code>
</Snippet>
</CodeSnippet>
The root
element for any code snippet is <CodeSnippet>. This root element has two
child elements <Header> and <Snippet>. Schema file for code snippet
is available at the following location,
C:\Program
Files\Microsoft Visual Studio 8\Xml\Schemas\snippetformat.xsd
The <Header> element
The <Header> element can be used to represent basic
characteristics of the code snippet. Following are the tags defined inside the
<Header> element
|
Tag Name
|
Description
|
|
Title
|
Displays title for code snippet
|
|
Shortcut
|
Defines key shortcut for code snippet. Using
this shortcut key, you can insert this code snippet in code editor with few key
strokes. After entering shortcut key if you press tab key then this code
snippet will be inserted into the code editor.
|
|
Description
|
Description for code snippet which will be
displayed when you select this code snippet in IDE
|
|
Author
|
Mention the author of code snippet. You can use
this tag if you are going to share this code snippet with others.
|
|
Snippet Type
|
Mentions the type of snippets. This tag is used
in C# code snippets ( like expansions)
|
The <Snippet> element
The <Snippet> element actually defines the code snippet.
This element is slightly more complicated than the <Header> element.
Following are the tags defined inside this element
|
Tag Name
|
Description
|
|
Reference
|
This specifies assemblies to be added to the
project when this code snippet is added to this project
|
|
Imports
|
This specifies the import statement that has to
be inserted at the beginning of the file.
|
|
Declaration
|
This mentions placeholders in the code for
replacement.
Placeholder declared in this element will be highlighted in the code when it
is inserted in to the code snippet. It has <Literal> and <Object>
tags inside it. Literal or Object element has id element to identity this
placeholder and tool tip element. This tooltip will come when you place your
cursor above that highlighted placeholder.
|
|
Code
|
This element declares the actual code of the
code snippet. It has language and scope attribute. Language attribute is to
specify the type of language this code is written. Scope is to define exactly
where this code can be inserted. Various options for scope attribute are Method
body, Method and Type.
|
Conclusion
This article explains about Code snippets in VS.NET
2005 and how you can use it in your application. In the next part of this
article, you will know how to create your own code snippets for your
applications. In second part, you will also see how you can share the code
snippets created by you amoung your team members.
|