Hello friends I want to share something on “Code Snippets” with you.
Code Snippet
Q: What is a “Code Snippet”?
A: A Code Snippet is a reusable block of code. Unlike a static copy-and-paste approach, code snippets allow for dynamic instances of code
by allowing placeholders into mini templates of code. Code snippets are a new feature of Visual Studio 2005.
Q: How do I use a Code Snippet?
A: In the Visual Studio 2005 code editor, you can invoke snippets with [ctrl][k][x] keyboard shortcut. IntelliSense will then display a
context menu displaying the available code snippets to choose. However, most code snippets are available without using the keyboard
shorcut. If you know the shortcut name of the code snippet, simply type the name and press tab to invoke.
Now we will see how we can use snippets here.
I will write a small snippet for disconneceted data access with dataset.
1) Open nottepad.
2) Type the code in it and save it as sample.snippet.
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Disconnected Data architecture</Title>
<Shortcut>Dataset</Shortcut>
<Description>Code snippet for Disconnected data access</Description>
<Author>Bharath Radhekrishna</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[dataset
SqlConnection cn = new SqlConnection();
string selquery = $selquery$;
SqlDataAdapter da = new SqlDataAdapter(selquery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
3) After saving this. Go to visual studio 2005. Click Tools > CodeSnippets manager or ctrl+K, ctrl+B.
and add this snippet to library.
4) While you are coding right click the mouse. You will see an option of “insert snippet”. when you click it will tell us
to insert the snippet we like. For example if take the above snippet it will be displayed with the name as:
“Disconnected Data architecture”.If we click it will display the code as:
SqlConnection cn = new SqlConnection();
string selquery = ;
SqlDataAdapter da = new SqlDataAdapter(selquery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
So everytime it is not necessary for us to write whole sentence or connections.It will reduce our burden of writing commonly used
code everytime.
For more info on Code snippets you can refer to:
http://gotcodesnippets.com/faq.aspx
http://www.google.com/search?q=how+to+insert+a+code+snippet+in+visual+web+developer+2005&sourceid=navclient-ff&ie=UTF-8&rlz=
1B3GGGL_enIN246IN246.
Hi!,
cool, thanks