How do you create Code Snippets, and what are they?[i]

Ok, ok, the space aliens are talking to me again, oh wait a minute, I found some aluminum foil… Made a hat, grounded it to a water pipe, now I am safe.

Let’s talk about code snippets, which is easier than talking to space aliens, although someday you and I will have that discussion about how to use software to communicate with space aliens. But for now, let’s talk about how to use code, and be an efficient software architect.

To be able to understand where the code comes from, most of my critics ask me to start off by showing what Code Snippets are, since many students and hobbyists do not use code snippets.

To keep this article short and sweet, or short and sweet as I can keep it, I will talk about:

· What code snippets are

· How to create the snippets

· How to add them to your project in C# Express 2005 (the instructions are not for C# Express 2008 or any version of Visual Studio Pro or Visual Studio Team Systems)

· How to use Code Snippets in your project.

In designing games, you will constantly be typing similar code all of the time, wouldn't it be nice to have a way to insert most of the code you need quickly and cleanly?

Well you can, using Code Snippets. I have included a file that contains code snippets, and I use them in my demonstrations, hobby work and real job.

How to Create a .snippet File[ii]

Before you can begin writing your code snippet, you must create an XML file with a .snippet file name extension. The process to create the code snippet file in Visual Studio Pro or Visual Studio Team System is well documented, however, the C# Express process is slightly different from the Visual Studio Pro or the Visual Studio Team System, and I have described it here.

In C# Express, to create a .snippet file, open notepad and cut and paste the file below and save it as <Filename>.snippet, the <filename> is anything you want to call it, but remember the folder you save it to. (Note: <![CDATA[ line is correct as written.)

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

<CodeSnippet Format="1.0.0">

<Header>

                <Title>

                                     ADD TITLE HERE THIS WILL APPEAR IN THE CODE SNIPPET INSERTION MENU

                         </Title>

          </Header>

         <Snippet>

                        <Code Language="CSharp">

                        <![CDATA[

                                          ADD CODE HERE, COPY AND PASTE FROM YOUR CODE

                                         ]]>

                        </Code>

           </Snippet>

</CodeSnippet>

</CodeSnippets>

How do you add the code snippets to your IDE?

You use the “Code Snippet Manager” under the Tools menu in the C# Express IDE (Integrated Design Environment). I know that there is some way you can use the code snippets in other environments, you can refer to the references to see how (the process at this point is basically the same): 

Here is the illustrated step by step:

Select the Add button and then navigate to the folder that holds your snippet, this folder name will appear in the location boxclip_image004

When you navigate to your code snippet folder, in this case the “Happy code snippets” folder. It will appear to be empty, I don’t get it either, but remember that the snippet has an extension of snippet.clip_image006

Now the folder, and if you click the little plus, you will see your code snippet folder. The file will show up as the “title” of your code snippet. If you look at the “title” of the file, you will see that it is AddPoint Code, and that the file name is “addpoint.snippet”.clip_image008

How to use your code snippets in a coding project

To use the code snippet in your code, right click where you want to add the code snippets and select “Insert Snippet” . Then select “AddPoint Code”, if there are more snippets, then you would select the correct one. Finally note the code has been inserted.clip_image010

Conclusion

There are other ways to use code snippets, and these are covered in the referenced articles, this article was presented to get you started in using code snippets.


[i] http://msdn2.microsoft.com/en-us/library/ms379562(VS.80).aspx#codesnippets_topic5

[ii] http://msdn2.microsoft.com/en-us/library/ms165392(VS.80).aspx