One of the cool features I love in Visual Studio is the Code Snippets which makes development easier in many ways. :) Let’s see how to create a code snippet and add to the snippets gallery in Visual Studio 2010. Just looking back at VS 2008, though it supports creation of code snippets it’s not an easy process without the use of 3rd party applications.
There are two snippet types…
Expansion – This type of snippet is inserted at the curser.
SurroundsWith – This type wraps around the selected code block.
Expansion – This type of snippet is inserted at the curser.
SurroundsWith – This type wraps around the selected code block.
Lets create a code file header snippet.
Add a XML file to your project and name it as verHeader.snippet. (.snippet is the extension of snippet files)
Add a XML file to your project and name it as verHeader.snippet. (.snippet is the extension of snippet files)
<SnippetType>SurroundsWith</SnippetType>
Modify the Title tag to read “Code File Header”.
Next put your name within the Author tags, so are the author of your code snippet. ;)
Modify the shortcut tag with “codehead” which is the rigger word that will activate the snippet.
”Adds a header to a code file” can be the text between the description tags. And yes I love C#, so I will choose the language attribute of the code as C#.
Now we need to alter the Literal section. Literals allow you to define editable values that are inserted into your snippet. I want the you to enter your own name in the author section, so change the ID value to Author and enter your name in as the Default tag.
Code section contains what will be added when you add the snippet.
<?xml version="1.0" encoding="utf-8" ?> <CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <Header> <Title>Code File Header</Title> <Author>Prabath Fonseka</Author> <Shortcut>codeHead</Shortcut> <Description>Adds a header to a code file.</Description> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal> <ID>Author</ID> <Default>Prabath Fonseka</Default> </Literal> </Declarations> <Code Language="CSharp"> <![CDATA[ /**************** Code File Header ****************** Author: $Author$ Date: Version: Description: ****************************************************/ ]]> </Code> </Snippet> </CodeSnippet>
Above is how your final snippet should look like.
Next step is to load the snippet in to Visual Studio which we have to do through Code Snippet Manager under the Tools menu. But by default (in VS 2010 RC) Code Snippets Manger does not comes under Tools menu so our next task is to add it under tools menu.
Go to Tools > Customize > Commands > Tools > Add Command…
By default Menu bar: radio button is selected, select Tools from the dropdown.
Go to Tools > Customize > Commands > Tools > Add Command…
By default Menu bar: radio button is selected, select Tools from the dropdown.
Add a class file in to your project. Move the curser to the first line Press Ctrl + K + X
Happy Coding.
Happy Coding.
2 comments:
Thanks! It's Working. But still i couldn't understand what is the use of it. Please tell me about?
code reuse, for example to insert into the database can be a code that called for 10 lines in a word
Post a Comment