asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

An Introduction to Code Snippets in Visual Studio - Part II
By John Peterson
Rating: not yet rated
Rate this article


  • email this article to a colleague
  • suggest an article


    The first part of this article introduced Visual Studio code snippets and showed how they can save you lots of time and typing by reducing the need for you to manually type repetitive code. It also showed you how to use shortcuts to make using snippets even faster. This installment will examine the .snippet file format and show you how to expand your snippet library by both downloading packaged snippets from the Web and creating your own snippets from scratch.

    What Does a .snippet File Look Like?

    Before we start installing more snippets or creating our own, it's a good idea to have a basic understanding of the .snippet file format.

    If you played around with the Code Snippets Manager that we discussed at the end of the last article, you may have noticed that when you select a snippet, it'll show you the filename which contains the snippet's declaration.

    Code Snippets Manager

    Since I used the "If...Else...End If" snippet for illustration last time, let's take a look at the actual .snippet file that makes it work. I've been using Visual Web Developer 2008 Express for this article and it stores the file in question at: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\VBExpress\Snippets\1033\common code patterns\conditionals and loops\IfElseEndIfStatement.snippet The file is just a regular XML-based text file so you can open it using Notepad, Visual Studio, or any other text editor. The contents of the file are below.

    IfElseEndIfStatement.snippet:

    <?xml version="1.0" encoding="UTF-8"?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
        <Header>
          <Title>If...Else...End If Statement </Title>
          <Author>Microsoft Corporation</Author>
          <Description>Inserts an If...Else...End If statement.</Description>
          <Keywords>
          </Keywords>
          <Shortcut>IfElse</Shortcut>
        </Header>
        <Snippet>
          <Declarations>
            <Literal>
              <ID>Condition</ID>
              <Type>Boolean</Type>
              <ToolTip>Replace with an expression that evaluates to either True or False.</ToolTip>
              <Default>True</Default>
            </Literal>
          </Declarations>
          <Code Language="VB" Kind="method body"><![CDATA[If $Condition$ Then

    Else

    End If]]></Code>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>

    The file is split into two main sections: "Header" and "Snippet". The "Header" section provides the basic information about the snippet. This is where you put the snippet's title, description, shortcut key, and other stuff like that. The "Snippet" section contains the actual code for the snippet as well as defining "Declarations" which are the editable areas which get highlighted in green when the user inserts a snippet.

    Downloading and Installing Additional Code Snippets

    I was actually relatively surprised that I didn't find more sources for snippets online. Everyone says they have code snippets, but very few actually have code available in Visual Studio's snippet format. One site that actually does is GotCodeSnippets.NET.

    The thing that's a little weird about downloading snippets is that they are usually packaged in the Visual Studio Community Content Installer (VSI) format. Simply save the .vsi file somewhere you'll be able to find it. Once you've downloaded it, simply double-click the file and you'll be greeted with a screen that looks something like this:

    Visual Studio Content Installer

    After you tell it where to install the snippet (I recommend you install all your downloaded and custom snippets in the "My Code Snippets" folder), it will copy the files to the appropriate places and display a status screen:

    Visual Studio Content Installer - Completed

    That's all there is to it. The downloaded snippet(s) are now installed and can be used just like any of the ones that shipped with your version of Visual Studio.

    Creating Your Own Snippets

    Sure Visual Studio includes lots of great snippets, and sure you can download lots more from the Web, but the real power of snippets is unleashed when you start creating your own. After all, only you know how you like to work and what you spend most of your time doing.

    Manually

    The format of .snippet files is simple enough that you can easily create your own snippets quite quickly by hand. Not wanting to type everything from scratch, I simply opened up an existing snippet and replaced the values with ones appropriate for what I wanted my snippet to do.

    For the sake of illustration, I've chosen to share one of the snippets that I find myself using quite a bit. It's short and I intentionally gave it a shortcut name that is the same as the IsPostBack property of the page object. I did it not only to show that you can, but also because it makes the shortcut almost impossible to forget.

    This snippet simply creates a conditional that checks to be sure the page is NOT being posted back. I quite often write this conditional so that code to initialize values and set the default state of objects on the page only runs when the page is first called. It's pretty simple so I'll just show you the complete .snippet file.

    IsNotPostBackCheck.snippet:

    <?xml version="1.0" encoding="utf-8"?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
        <Header>
          <Title>IsNotPostBack Check</Title>
          <Author>15Seconds.com</Author>
          <Description>Creates a conditional to check that a page is not being posted back.  Useful for initialization.</Description>
          <HelpUrl>http://www.15seconds.com/issue/080710.htm</HelpUrl>
          <Keywords />
          <Shortcut>IsPostBack</Shortcut>
        </Header>
        <Snippet>
          <Declarations>
            <Literal>
              <ID>InitCode</ID>
              <Type>String</Type>
              <ToolTip>Replace with code to run if page is running for the first time.</ToolTip>
              <Default>' Initialization Code Goes Here</Default>
            </Literal>
          </Declarations>
          <Code Language="VB" Kind="method body"><![CDATA[If Not IsPostBack Then
        $InitCode$
    End If]]></Code>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>

    Once you've completed your changes to your new .snippet file all you need to do is place it into the "My Code Snippets" folder located under the Visual Studio folder in "My Documents". The full path should be something like: C:\Documents and Settings\_Your_User_Name_\My Documents\Visual Studio 2008\Code Snippets\Visual Basic\My Code Snippets. Once the file is there, if everything is working correctly Visual Studio should automatically find it and make it available for you to use.

    Here are a couple screen caps of the snippet in use.

    The "IsNotPostBack Check" Snippet In Use

    The "IsNotPostBack Check" Snippet In Use

    Code Snippet Editor

    If you're more of a GUI type of person, there are also some tools floating around to help you create snippets without having to write the XML yourself. The Code Snippet Editor for Visual Basic 2008 is one such tool. You can find out more about it and download a copy from here: http://msdn.microsoft.com/en-us/vbasic/bb973770.aspx

    The interface is quite straight-forward and you shouldn't have any problems finding your way around the tool. So you can get an idea what it looks like, here's a screen shot of the tool with the "IsNotPostBack Check" sample snippet created loaded for editing.

    Code Snippet Editor for Visual Basic 2008

    If you have any trouble with the way the tool manages snippets, don't forget you can always use the import functionality of Visual Studio's Code Snippets Manager or simply copy your .snippet file to the "My Code Snippets" folder located in your "My Documents" folder just like we did with the one we created by hand.

    Conclusion

    The first part of this article introduced Visual Studio code snippets and showed how they can save you lots of time and typing by reducing the need for you to manually type repetitive code. It also showed you how to use shortcuts to make using snippets even faster. This part showed how you can take the time savings to a whole new level by downloading and creating your own snippets.

    Snippets are one of the greatest time savers to come along in a long while. If you haven't already been using them I hope this article has helped open your eyes. If you have, hopefully you still learned something new and useful about these powerful little coders.

    Additional Information

    For more information on Visual Studio Code Snippets you may find these links helpful.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 21, 2005 - N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005 - Part 1
    While the .NET Framework made building ASP.NET applications easier then it had ever been in the past, .NET 2.0 builds on that foundation in order to take things to the next level. This article shows you to how to construct an N-Tier ASP.NET 2.0 Web application by leveraging the new features of ASP.NET 2.0 and SQL Server 2005.
    [Read This Article]  [Top]
    Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
    With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
    [Read This Article]  [Top]
    Mar 10, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2, Cont'd
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 9, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 3, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1, Cont'd
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Mar 2, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Feb 16, 2005 - Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
    In ASP.NET 2.0 and Visual Studio 2005, you can quickly program custom authentication pages with the provided Membership Login controls. In this article, Dina Fleet Berry examines the steps involved in using the Login control with a custom SQL Server membership database.
    [Read This Article]  [Top]
    Dec 29, 2004 - ClickOnce Deployment in .NET Framework 2.0
    In this article, Thiru Thangarathinam examines .NET 2.0's new ClickOnce deployment technology that is designed to ease deployment of Windows forms applications. This new technology not only provides an easy application installation mechanism, it also eases deployment of upgrades to existing applications.
    [Read This Article]  [Top]
    Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
    With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
    [Read This Article]  [Top]
    Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
    Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers