Monday, July 23, 2007

Code Snippets in VS 2005

Code Snippets is a really cool feature in microsoft visual studio 2005.It enables you to store pieces of code to use in a later time givinning you the chance to store, share, publish and reuse that code.The idea is simple you store your code in an xml file with a predefined scheme and save it with .snippet extention.

Visual studio Itself comes with many code snippet written for you and ready to use.You can see all code snippets from the Code Snippets Manager of the Tools menu where you can add your own folders of code snippets.
In these steps I'll show you how to create code snippets in visual studio and introduce you to the schema and later on how to use snippets from visual studio

Crate a code snippet:

1- Add an xml file from add new item menu.
2- change the extension from .xml to .snippet
3- Under the processing instruction line of the xml file (which is the first line containg the version) write
4- you will find the schema craeted for you.all you need to understand It and replace the place holders with your own.

Understanding The Scheme
The xml scheme for code snippet is very easy:

The root elment is CodeSnippet which contains a version an a namespace. This tag contains two main tags (elements) which we will do our changes in, Header and Snippet.

Header
contains the following elements:

Title
The name of the element.

Author
The author of the code (may be your company name or your name)

Shortcut
The shortcut that you will use to reach your snipppet faster by typing your shortcut and press TAB key twice.

Description
The help alt text that will appear when user select the snippet.

SnippetTypes
Indicates how visual studio iserts the code snippet, at the cursor or surrounding a selected code.

Snippet
contains elements that includes the actual code

Imports
Imports the specified namespace when the code snippet is inserted.

Declarations
Contains Literal elements which acts as varables for the place holders used in your code snippet.
Each Literal contains ID which is the placeholder variable,Default which is the default value for that placeholder and a ToolTip which is the alt help text for that place holder.

Code
That's where our code goes.It has a lnguage attribute to specift the language which may be C#,VB,JScript or XML.

[CDATA]
here you can put your code and place the Literals ($placeholders$) between $ dollar signs or any other character you specify in the delimiter attribute of the Code element.

here is a print screen of default Scheme:




Now you have created your Code Snippet, How can you use It in Visual Studio:
1-Save your Code snippet file in a folder.
2-From Tools select Code Snippet Manager.
3-In Code Snippet Manager window choose your language and then click Add button and choose your folder.

Like that:







4-Whenever you want to use your piece of code you have two ways:

Right click your mouse in the code window where you want your code, select insert snippet , choose your folder and then your code which named as you put in the title element.



OR

write the shortcut(as specified in the shortcut element of the scheme) of your snippet and press
TAB key twice.

Visual studio comes with many snippets ready made for you like if statement.
Try It out,write down an if and then Press TAB twice and you will see.

And That's It...
Enjoy...

2 comments:

Usama Nada said...

Very New topic to me
Thanks for this cool post

Anonymous said...

very interesting Basem
thank you (doulsy)