Code Snippets in Visual Studio .Net 2005 by azamsharp

Introduction:

How many times you wrote the same code again and again? How many times you knew the whole code but could not get enough time to write it? This is about to be over now since in Visual Studio.NET 2005 you can take advantage of Snippet Manager to quickly insert code fragment into your code.

In this article we will see how easy it is to make custom snippets.

What are Code Snippets?

Code Snippets are nothing more than small pieces of code that you often use. These code snippets can range from a simple for-loop to complex methods and properties. There are several code snippets already build into Visual Studio.NET 2005. These snippets include loops, conditional statements, interfaces and etc.

Let’s see some of the build in code snippets in Visual Studio.NET 2005 in action.

The screen shot above represents one of the built in code snippets. Yes, you have guessed it right its a simple for loop. In order to create this for loop quickly simply press “f” and the intellisense will open select the “for” keyword and then press “TAB” two times. That’s it and the snippet will be put on your screen. Pretty cool right !!

Not only that but you can further press tab to insert the value of the variables and change the name of the variables. You can use the same technique with foreach loop, if-else statements and so on.

Let’s get to the fun part where you can create your own custom code snippets.

Creating a Simple Custom Code Snippet:

I know you are all excited about creating your own custom snippets so let’s jump to the fun. All you need to create the snippet is an xml file which ends with the .snippet extension. After making the .snippets file simply place it in the Code Snippets folder of your Visual Studio.NET 2005. My folder location is C:Documents and SettingsAZAMSHARPMy DocumentsVisual Studio 2005Code SnippetsVisual C#My Code Snippets.

Please note that the folder location might be different on different machines depending on where you installed the Visual Studio.NET 2005.

Let’s start with a very simple code snippet which creates a property which returns “Hello World” to the client. Here is a simple xml file.

<?xmlversion="1.0"encoding="utf-8" ?><CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

<CodeSnippetFormat="1.0.0">

<Header>

<Title>Hello World</Title>

</Header>

<Snippet>

<References>

<Reference>

<Assembly>System.dll</Assembly>

</Reference>

</References>

<Imports>

<Import>

<Namespace>System.Data.SqlClient</Namespace>

</Import>

</Imports>

<Declarations>

<Literal>

<ID>PropertyName</ID>

<Default>HelloWorld</Default>

</Literal>

</Declarations>

<CodeLanguage="csharp"> 

<![CDATA[ 

public string $PropertyName$

{

get { return "Hello World"; }

}

]]>

</Code>

</Snippet>

</CodeSnippet>

</CodeSnippets> 

The Title tag represents the title which you will see when you add the snippets from the snippets manager. You will see what I mean in a little while. I have bolded out the important part of the .snippets file. <declarations> tag contains the declarations of all the variables that we are doing to use in order to make our snippet. These variables can be substituted in Visual Studio.NET 2005 by pressing the TAB key and iterating over the variables of the snippets. The Code Language tag specifies the language of the snippet which in this case is csharp. The CDATA section contain the actual code for the snippet. As you can see that I am not doing anything fancy I am simply returning “Hello World” to the client and that’s it.

After making the .snippets file put it under the Code Snippets folder which might be at C:Documents and SettingsAZAMSHARPMy DocumentsVisual Studio 2005Code SnippetsVisual C#My Code Snippets.

Inserting Code Snippet into the code:

Now, you are ready to use your custom snippet in your code. There are numerous ways in which you can enter the snippet in your code here are couple of them.

You can select Tools option from Visual Studio.NET 2005 and select Code Snippet Manager. This will open a new window which will allow you to select your code snippet.

Another and faster way is to simply press Cntrl K and X. This will open a small intellisense which will allow you to paste your snippet. Take a look at the screen shot below to have a clear idea of the second technique.

Here is another screen shot which shows the snippet added to the code.

Pretty cool right!

Creating Advanced Snippets:

Let’s create some advance snippets.
We are going to convert the following code to snippet.

public string UserName{

get 
{

if (!String.IsNullOrEmpty(_userName))

return _userName;

else return String.Empty;

}

set { _userName = value; }

}

Now let’s see the XML file:

<?xmlversion="1.0"encoding="utf-8" ?><CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/

2005/CodeSnippet">

<CodeSnippetFormat="1.0.0">

<Header>

<Title>String Property</Title>

</Header>

<Snippet>

<References>

<Reference>

<Assembly>System.dll</Assembly>

</Reference>

</References>

<Imports>

<Import>

<Namespace>System.Data.SqlClient</Namespace>

</Import>

</Imports>

<Declarations>

<Literal>

<ID>UserName</ID>

<Default>UserName</Default>

</Literal>

<Literal>

<ID>PrivateField</ID>

<Default>_userName</Default>

</Literal>

</Declarations>

<CodeLanguage="csharp"> 

<![CDATA[ 

public string $UserName$

{

get

{

if (!String.IsNullOrEmpty($PrivateField$))

return $PrivateField$;

else return String.Empty;

}

set { _userName = value; }

}

]]>

</Code>

</Snippet>

</CodeSnippet>

</CodeSnippets> 

All the XML files that I have included in this article are embedded in article and not available as a separate file. Simply copy and paste the above XML into a .snippet file and that’s it.

Here is another XML file that allows you to return the DataSet.

<?xmlversion="1.0"encoding="utf-8" ?><CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/

2005/CodeSnippet">

<CodeSnippetFormat="1.0.0">

<Header>

<Title>GetDataSet</Title>

</Header>

<Snippet>

<References>

<Reference>

<Assembly>System.dll</Assembly>

</Reference>

</References>

<Imports>

<Import>

<Namespace>System.Data.SqlClient</Namespace>

</Import>

</Imports>

<Declarations>

<Literal>

<ID>METHOD</ID>

<Default>BindData()</Default>

</Literal>

<Literal>

<ID>SQLCONNECTION</ID>

<Default>myConnection</Default>

</Literal>

<Literal>

<ID>CONNECTIONSTRING</ID>

<Default>ConnectionString</Default>

</Literal>

<Literal>

<ID>ADAPTER</ID>

<Default>ad</Default>

</Literal>

<Literal>

<ID>QUERY</ID>

<Default>query</Default>

</Literal>

<Literal>

<ID>QUERYTEXT</ID>

<Default>SELECT * FROM Categories</Default>

</Literal>

<Literal>

<ID>DATASET</ID>

<Default>ds</Default>

</Literal>

<Literal>

<ID>TABLENAME</ID>

<Default>Categories</Default>

</Literal>

</Declarations>

<CodeLanguage="csharp"> 

<![CDATA[ 

private DataSet $METHOD$

{

// make the query

string $QUERY$ = "$QUERYTEXT$";

SqlConnection $SQLCONNECTION$ = new SqlConnection($CONNECTIONSTRING$);

SqlDataAdapter $ADAPTER$ = new SqlDataAdapter($QUERY$,$SQLCONNECTION$);

DataSet $DATASET$ = new DataSet();

$ADAPTER$.Fill($DATASET$,"$TABLENAME$");

return $DATASET$;

}

]]>

</Code>

</Snippet>

</CodeSnippet>

</CodeSnippets> 

Try the above XML file and see what it generates.

So, you see making code snippets can really save you time. You can use it to create any snippets which you frequently use.

I hope you liked the article, happy coding!