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
By John Peterson
Rating: 4.1 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Sometimes it's the little things that can really make your day. That's the way I feel about Visual Studio's Code Snippets. Ever since they were introduced in Visual Studio 2005, I've wondered how I ever wrote code without them.

    Note: More so then most things in Visual Studio, code snippets behave slightly differently between languages. Since most of our readers code in VB, I'll be using it for the code examples and screen captures throughout the article. If you prefer C#, not to worry. The same concepts apply, but you may run across a few things which function slightly differently then illustrated.

    What Are Code Snippets?

    Code snippets are exactly what they sound like... little pieces of code that you can use to help you build your pages faster. They're better then cutting-and-pasting from a library of code because they include dynamic placeholders for the sections of code that you'll need to edit. Since the functionality is built right into the Visual Studio IDE, they're also much faster then the cut-and-paste approach (especially once you learn the shortcuts).

    Inserting a Code Snippet

    Let's examine the process of inserting a basic code snippet. To begin the process, simply right-click where you want the code to appear in the code window. This will bring up the context menu. From the menu, select "Insert Snippet..."

    Right-Click and "Insert Snippet..."

    This will bring up the Insert Snippet box. From here you can drill down into the different snippet categories and select the snippet you want to insert.

    In this case, I'm going to chose the simple "If... Else... End If Statement" snippet. Notice that each snippet will also you its "Shortcut". A shortcut lets you invoke a snippet without needing to go through the whole process I'm outlining here. We'll discuss shortcuts more in the next section. I just wanted to show you one way you can find a specific snippet's shortcut.

    Once you select a snippet, Visual Studio will insert the corresponding block of code into the code window. The block of code will usually contain at least one section that you'll normally need to edit. These sections are highlighted in green and if you hover over them with the cursor, you'll get a tooltip telling you what type of expression should go there.

    While the code for the snippet I picked was relatively short and simple, there's no reason it has to be short. The code for some of the snippets is quite long. For example, the snippet for drawing a pie chart in a Windows Form application contains two subroutines. I mention this so that you realize you're free to do as much or as little as you'd like in a snippet.

    Shortcuts

    While what I just showed you is definitely cool, if you had to go through the whole snippet folder process every time, it'd get old pretty fast. Not to mention it would slow you down to the point where the snippets probably wouldn't save you very much time at all. Luckily Visual Studio contains a number of shortcuts that make using snippets much quicker. The first one is actually called a "Shortcut", and most snippets have one.

    A "Shortcut" is a short keyword that you can type in to the code window in order to insert the corresponding snippet. If you didn't notice, the "Shortcut" for the "If... Else... End If Statement" snippet I used earlier is "IfElse". To use the "Shortcut", all you need to do is type "IfElse" and then press the <Tab> key. This screen was captured right before I pressed <Tab>.

    The same screen a second later after I pressed <Tab>.

    What if you're not sure of a "Shortcut"? Well that's no problem either. Simply type the starting portion of the shortcut (at least one letter), follow it with a question mark ("?") and then the <Tab> key. It sounds confusing, but after you've done it a few times it becomes second nature. That'll bring up a list of all the registered shortcuts. From there you can scroll to the one you want and select it from the list.

    The other time saver that you may find useful (especially until you learn some shortcuts) is the <Ctrl-K>, <Ctrl-X> key combination. It'll bring up the "Insert Snippet" browser without you needing to right-click to bring up the context menu or go to the "Edit -> IntelliSense -> Insert Snippet..." menu.

    The Code Snippets Manager

    The next step in dealing with snippets is the "Code Snippets Manager". You can get there by going to "Tools -> Code Snippets Manager..." or by using the <Ctrl-K>, <Ctrl-B> key combination.

    Code Snippets Manager

    The "Code Snippets Manager" lets you browse the available snippets and read their descriptions. It'll also tell you where a particular snippet's file is located and allows you to tell Visual Studio which folders it should watch for snippet files.

    We'll talk more about "Code Snippets Manager" in the next installment. For now I just wanted to let you know it's there and that you can use it to browse through the installed snippets.

    Conclusion

    I hope this article has helped you see just how simple and powerful Visual Studio's Code Snippets are. Next time I'll cover downloading and installing additional snippets as well as creating your own both manually and by using the Code Snippet Editor.

  • 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



    JupiterOnlineMedia

    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