Sections
     
     
WWWCoder.com Resource Directory

Code Snippets in Visual Studio 2005
1/10/2006 10:55:39 AM

Want to save some time writing code for that next big project? The new code snippets feature in Visual Studio 2005 will enable the developer to have a toolset of commonly used code for performing various functions.

Want to save some time writing code for that next big project? The new code snippets feature in Visual Studio 2005 will enable the developer to have a toolset of commonly used code for performing various functions.

The Code Snippets Manager

Next time your in your development project in Visual Studio 2005, check out the Code Snippets Manager. This tool can be accessed by going to the Tools | Code Snippets Manager... menu option with Visual Studio 2005.

Code Snippets Manager
Figure 1 - The Code Snippets Manager

You'll notice a folder listing broken down into various types of functions you may want to perform in your application. For example, by clicking on the Connectivity and Networking folder, you'll find functions like sending an email message or reading data from a serial port.

There are several buttons available which we'll talk about the obvious one is Remove which lets you click on a snippet or folder and remove the snippet from the Manager. In addition you can Add or Import new snippets, and finally search for snippets available online.

Using a Snippet

Let's go about using one of these snippets.

Place your cursor in your code where you want to place the snippet.

Right click and select Insert Snippet from the context menu. This will bring up a drop where you can navigate to the code snippet you wish to use. You will see your path to the snippet being built on your code page as select the item (see Figure 2).

Adding Code Snippets

Once you select your code snippet, the generated code will be placed in your code, as in the following example:

Dim message As New MailMessage("sender@address", "from@address", "Subject", "Message Text")
Dim emailClient As New SmtpClient("Email Server Name")
emailClient.Send(message)

The snippet tool will place the necessary code for sending the message, in addition it will highlight the values that you should change within your application.

Creating a Snippet

Snippets are formatted XML files that contain your code as well as any other descriptive information about the code. This XML file has the .snippet extension and is structured in the following manner:

<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>
                My Snippet
            </Title>
        </Header>
        <Snippet>
            <Code Language="VB">
                <![CDATA[MessageBox.Show("Hello World")]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

 

You can see the XML file is broken down into a Header section which contains the title to describe your function, and the Snippet section to contain your code. Also note that the code is contained within a CDATA section.

Once you have created your snippet, you can now import the snippet using the Code Snippet Manager as in figure 1 of this article. Just save your XML file using the .snippet extension, then click the Import button within the Code Snippet Manager. Navigate to your file and select it in order to import into the Manager. You should now see your code snippet.

You'll find code snippets are an excellent way of organizing code you repeatably use within your applications.

 


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 10/8/2009 3:12:51 PM by Anonymous
Comments: Hey, neat, it has auto-complete for 'if'. Now it takes three keystrokes instead of two, since I have to confirm the replacement. Another helpful innovation from Microsoft.

Seriously though, you can disable this idiocy if you hunt through the options, and configure Studio to not suggest keywords, and not suggest anything at all until your ctrl+space or whatever you use.
Left on 10/8/2009 2:52:59 PM by Anonymous
Comments: Does not say how to remove or disable this useless feature. Thanks Visual Studio, now I can auto-complete "else" to "else" with just a single extra keystroke. You numb cunts.
Left on 1/8/2008 9:53:51 AM by Anonymous
Comments: I was looking for just the basic info and this article provided it.
Left on 8/16/2007 11:46:10 AM by Anonymous
Comments: Not to be stupid or anything, but once you understand this... wouldn't you just make a snippet for making snippets?

Nice enough article, but it just scratches the surface. As others have said, it would be better if it followed up with a more detailed explanation of all of the features.
Left on 6/14/2007 7:24:11 AM by Anonymous
Comments: very worst
Left on 1/28/2007 11:30:06 PM by Anonymous
Comments:
Left on 11/8/2006 1:12:42 PM by Anonymous
Comments: really great

Left on 9/21/2006 7:45:46 AM by Anonymous
Comments: A simple Code Snippet editor available on http://www.gotdotnet.com .

More samples and explanation shud have proved better.
Left on 5/1/2006 4:04:56 AM by Anonymous
Comments: given more example to understanding how we use it
Left on 2/14/2006 2:44:42 PM by Anonymous
Comments: very basic level of info
Left on 1/30/2006 6:36:17 PM by Anonymous
Comments: visit http://www.gotcodesnippets.net
Left on 1/30/2006 10:41:43 AM by Anonymous
Comments: Code Snippets - Great Idea - Implementation Really Sucks!

Working with the supplied snippets is easy, the real problem is storing and retrieving your own snippets.

Developed by anyone other than MS, this feature would simply have had a [Save Snippet] button which would allow you to save your selected piece of code AND ask you to fill in the additional info on a form contained within the IDE.

However, let loose a bunch of geeks on steroids and you get something over-designed, over-complicated, and a user interface which is guaranteed to increase your stress levels and induce premature baldness in the most painful way possible.

Left on 1/29/2006 6:20:42 AM by Anonymous
Comments: PS for those of you who don't know how to insert the snippet to be available inside vs 2005, just put the snippet file in the the "My Documents\Visual Studio 2005\Code Snippets\Visual Basic\My Code Snippets\" directory or any subdirectory you might want inside. Then it will automatically be availeble for you in the My snippets section of your "Insert Snippet" Menu
No ratings available.
Left on 1/29/2006 6:17:09 AM by Anonymous
Comments: I would like to have this article to be more in depth, The basics are well explained but it should go further, for instance it should teach how to highlight code
Left on 1/27/2006 12:59:15 PM by Anonymous
Comments: Hey Anon, it's available in my menu, what's up?
No ratings available.
Left on 1/13/2006 8:51:30 AM by Anonymous
Comments: It would be a nice feature if it were actually avaialble from the menu in Visual Studio Pro 2005
     
     

 

     
     

 


 


Digg This
 


DotNetNuke Platinum Benefactor

     
     

Other family network sites: santry.com - katieandkarleigh.com

Powered by 

 

     
Copyright 20010 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970
Privacy Statement | Terms Of Use