|
Code
Snippets in VS.NET 2005 - Part 2
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 explains how to create your own code snippets and in the
first part of this article I have described how to use code
snippets feature in Visual Studio .NET 2005 (VS.NET) .
Create your own Snippets
Other than the pre-defined code
snippets provided by VS.NET, you could create your own code snippets. There are
two ways to create your own snippets,
-
Creating
code snippet (.vbsnippet) file according to code snippet XML structure and
registering that code snippet file with VS.NET
-
Creating
code snippet using Snippet Editor
Creating Code Snippets manually
If
you understand the code snippet XML structure, then you can write your code
snippets very easily. There are two ways to create .vbsnippet file manually.
First way is to copy the existing .vbsnippet file and then change it for your
need. Second way is to write it from scratch. Modifying an existing file is the
simplest way and this will avoid complication of creating XML structure
according to snippet schema.
Consider
if you want to create snippet for DataGrid
binding code. According to snippet schema, code snippet for
DataGrid binding is shown below;
<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets>
<CodeSnippet Format="1.0.0">
<Header>
<Title>MyCustomSnippet</Title>
<ID>{13A00833-759E-4470-A473-6891E0039494}</ID>
<Version>1.0.0.0</Version>
<Locale>en-us</Locale>
<Author>hp</Author>
<SnippetTypes>VB</SnippetTypes>
</Header>
<Snippet>
<Platform>
<Runtime>v2.0.40607</Runtime>
</Platform>
<References>
<Reference>
<Assembly>System</Assembly>
</Reference>
<Reference>
<Assembly>System.Web</Assembly>
</Reference>
</References>
<Imports>
<Import>
<Namespace>System</Namespace>
</Import>
</Imports>
<Declarations>
<Literal>
<ID>DGName</ID>
<Type/>
<ToolTip>Enter DataGrid Name</ToolTip>
<Default>DataGrid1</Default>
<Function/>
</Literal>
<Literal>
<ID>DSName</ID>
<Type/>
<ToolTip>Enter DataSet Name</ToolTip>
<Default>oDS</Default>
<Function/>
</Literal>
</Declarations>
<Code Language="VB" Kind="method body">
<Literal ID="DGName">DataGrid1</Literal>.DataSource =
<Literal ID="DSName">oDS</Literal>
<Literal ID="DGName">DataGrid1</Literal>.DataBind()
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
After you have created the
.vbsnippet file, you can place it in any folder in your machine. But if you
place it in a folder other than the default code snippet folder (C:\Program
Files\Microsoft Visual Studio 8\Vb\Snippets\1033 ) you will need to register
that folder with VS.NET, so that VS.NET will pick up that snippet file along
with other pre-defined snippet files.
For registering or removing a
snippet folder with VS.NET, you can use snippet manager. You can invoke Snippet
managers from Tools Menu (Toolà
Code Snippet Manager). Once invoked, you are shown a dialog box, which allows
you to register the snippet folder with the VS.NET. Best approach is to create
a snippet folder and place your custom snippet files there. Then register that
folder with the snippet manager. Code
Snippet manager dialog box is shown below;
Inserted code of this code snippet in
code editor will look like this,

You just need to tab through the replacement points
and change DataGrid name and DataSource name.
Creating Code Snippets using Snippet Editor
The other option to create custom folder is by
using Snippet Editor. In this method, you need not worry about the snippet XML
structure. This tool will create the .vbsnippet
file for you. You only need to mention which code you want to put in the code
snippet file and what are the replacements point in that code. Here are the
steps for creating code snippet using snippet editor,
-
In
the VS.NET, Open Code Editor window.
-
In
the Code Editor, select the code that you want to add to the snippet.
-
Right-click
and select Create Snippet. The code will be copied to the Snippet Editor.
Snippet Editor is shown in the following figure,
-
Modify
the code as desired in the Snippet Editor.
-
The
Snippet Editor will detect and add the project references and Imports from the
project that the code was copied from. Use the Review References pane to modify
the selections.
-
To
create replacement points in your snippet, select the replacement text that you
want highlighted when the snippet is inserted. Right-click and select Create
Replacement. In the Replacement panel, enter the ID, Type, Object Type, and
Tooltip. The Object Type is only relevant if you have selected Object as the
Type.
-
Use
the Properties pane to specify the Title, Author, Version, Help Page, Keyboard
Expansion Shortcut, Description, and Keywords.
-
From
the File menu, choose Save Snippet or Save Snippet As to save the snippet file.
The code snippet files created using the snippet editor are
stored in C:\Documents and
Settings\<username>\My Documents\Visual Studio\Whidbey\CodeSnippets\VB
Snippets. By default this folder will be recognized by VS.NET Code
Snippet Manager, so you need not register this folder again with Code snippet
manager.
Sharing Snippets among team members
Custom snippets can be shared among
team member by placing that code snippet (*.vbsnippet)
file in a shared network location and then registering that folder with code
snippet manager in each member machine. In future, third party will also
publish snippet files in the internet. In that case, you can download these
files into one common location and the entire team members can have access to
this.
Things to consider when using this feature
Code snippet is one of the important
features introduced in VS.NET 2005 for rapid programming. This feature
shouldn’t be used in place of copy paste programming which has lots of
problems. If you place similar code again and again in your application, then
maintaining that code will become a major problem. Hence you need to look for
refactoring options. Similarly you shouldn’t use code snippets simply in all
the places, check whether you can refactor your code instead of using code
snippet. So that code replication can be avoided.
Conclusion
Visual
Studio .NET 2005 is coming up with many predefined code snippets. These
snippets increase your productivity by reducing the amount of coding you need
to perform. In addition to leveraging the supplied code snippets, developers
are free to create their custom code snippets, which they can share among
others. This Knowledge brief explores only code snippet feature of VB.NET, a
similar feature is also available in C#.
|