Efficiency of programming allows us to spend more time designing and securing our code, as well as not working 80 hours a week.

Built in Code Snippets can usually be found at a location similar to:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#\if.snippet

For this blog, you will need to use one of the existing snippets, the file extension is *.snippet.

In this discussion I am going to use the “if” snippet, which has the following XML

Code Snippet
  1. <?xml version="1.0" encoding="utf-8" ?>
  2.   <CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.     <CodeSnippet Format="1.0.0">
  4.         <Header>
  5.             <Title>if</Title>
  6.             <Shortcut>if</Shortcut>
  7.             <Description>Code snippet for if statement</Description>
  8.             <Author>Microsoft Corporation</Author>
  9.             <SnippetTypes>
  10.                 <SnippetType>Expansion</SnippetType>
  11.                 <SnippetType>SurroundsWith</SnippetType>
  12.             </SnippetTypes>
  13.         </Header>
  14.         <Snippet>
  15.             <Declarations>
  16.                 <Literal>
  17.                     <ID>expression</ID>
  18.                     <ToolTip>Expression to evaluate</ToolTip>
  19.                     <Default>true</Default>
  20.                 </Literal>
  21.             </Declarations>
  22.             <Code Language="csharp"><![CDATA[if ($expression$)
  23.     {
  24.         $selected$ $end$
  25.     }]]>
  26.             </Code>
  27.         </Snippet>
  28.     </CodeSnippet>
  29. </CodeSnippets>

First change the filename, you don’t want to change this file, temporily, I am changing mine to:

  • C:\Users\yourname\Documents\Visual Studio 2010\Projects\Snippet\variables.snippet

Using the snippet file above I changed it to the following and after making the change I placed it in the folder:

  • C:\Users\sstokes\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets
Code Snippet
  1. <?xml version="1.0" encoding="utf-8" ?>
  2.   <CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.     <CodeSnippet Format="1.0.0">
  4.         <Header>
  5.             <Title>Paddle Variables</Title>
  6.             <Shortcut>pvariable</Shortcut>
  7.             <Description>Paddle variables</Description>
  8.             <Author>Sam stokes</Author>
  9.             <SnippetTypes>
  10.                 <SnippetType>Expansion</SnippetType>
  11.                 <SnippetType>SurroundsWith</SnippetType>
  12.             </SnippetTypes>
  13.         </Header>
  14.         <Snippet>
  15.             <Declarations>
  16.                 <Literal>
  17.                     <ID>Variables</ID>
  18.                     <ToolTip>Use when designing paddle games</ToolTip>
  19.                     <Default>true</Default>
  20.                 </Literal>
  21.             </Declarations>
  22.             <Code Language="csharp">
  23.         <![CDATA[
  24.               Texture2D t_paddle1, t_paddle2, t_ball;        
  25.                     Random rand = new Random();]]>
  26.             </Code>
  27.         </Snippet>
  28.     </CodeSnippet>
  29. </CodeSnippets>

 

Once everything is working, you will be able to do the following, select pvariable (the code snippet name) then press tab twice

image

You get the following placed in your code:

image

Pretty easy.  If you have issues with the code entered make sure you stripped out all of the old code from the snippet you used as a “template”.  Generally keep all of the other items, change the Author from Microsoft to your name.  Play around with this feature, which could do with a little more videos and explanation. from Microsoft.  Oops, I am a Microsoft employee, but I meant the development team.  However, if you use existing snippets as starting points then you shouldn’t have any big issues.  Just make sure to save your snippet to the C# My code snippet folder or VB My code snippet folder, also there is one for XML, which come to think of it is quite useful.  There are no code snippets for VC++, sadly.

 

Well have fun.