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.)
ADD TITLE HERE THIS WILL APPEAR IN THE CODE SNIPPET INSERTION MENU
ADD CODE HERE, COPY AND PASTE FROM YOUR CODE
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:
How to use your code snippets in a coding project
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.