Ropardo Sowftware development company

Experience software development with ROPARDO S.R.L.

RSS Feed
RSS Feed
  • Home
  • About ROPARDO S.R.L
  • Our websites

Code Snippets

The Visual Studio Code Snippets feature allows very simple, common, code blocks to be reused without requiring the same thing to be typed out repeatedly, or copied and hacked. The Code Snippets feature allows inserting code, wrapping existing code and replacement of key parts of the Snippet.

The snippets themselves, of course, are just individual XML files that end in .snippet sitting in a bunch of folders

  • Using Visual Studio default snippets (C#)

Some of the Code Snippets that are included with Visual Studio include properties, constructors, code blocks etc. You can find a list of built-in snippets here: http://john-sheehan.com/blog/cheatsheets/visual-studio-2008-csharp-snippets.htm. There are not a lot of them, but they are very useful once you’ve got accustomed to using them. Also, Microsoft have released a whole bunch of C# snippets that you can download from http://msdn.microsoft.com/en-gb/vstudio/aa718338.aspx

Remember that there are no “best” code snippets. There are snippets that are more “useful” than others, but no “best” snippets.

The easiest way to use a code snippet is to begin typing part of the snippet shortcut name, eg “prop” for property expansion. Once you’ve typed enough to uniquely select the snippet shortcut name, press: Tab, Tab and the snippet will be autocompleted.

If it’s a templated snippet Tab advances to the next field, Shift+Tab moves to the previous field, Enter completes the snippet and resumes editing at your previous cursor position

If you want to enclose code in a surrounding snippet (eg, you have code highlighted for a region), you must manually invoke snippet intellisense by pressing: Ctrl+K, Ctrl+X then select the snippet you need (#region, for, if etc) and press Tab, Tab.

  • Creating and using custom snippets

You can either create new snippets or edit the built-in ones to suit your needs. (The Visual Studio snippets can be found at C:\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#.)

I found using custom snippets feature particularly useful when working extensively for a long period of time with a proprietary framework that handles common functions like displaying a custom message, getting various languages text resources etc. I can just add snippets for the code that is used very often, and voila! A piece of code in just a few clicks!

If you don’t want to get you hands “dirty” by creating / editing XML files directly, you can use a snippet editor (e.g. http://www.codeplex.com/SnippetEditor) that does all the XML formatting done, is easy to use and you can have fun with just the code.

Save the new snippet file that you created somewhere where you can locate it easily and switch to Visual Studio.  Bring up the Code Snippets Manager with the keyboard shortcut chord Ctrl+K, Ctrl+B and add the folder where you saved your new snippet.

Once the Snippets have been added they can be accessed from the Snippets context menu (ctrl-k, ctrl-x or right click > insert snippet) or via shortcuts. The context menu will present the Snippets in the folder structure setup in the snippet manager.

It may take a bit of time to get accustomed to using them and creating your own, but on the long term it will be well worth it when they get into your work routine. Code Snippets can save you precious development time and save you from the frustrating, tedious reproduction of well-known code constructs that can make development really boring and annoying at times.

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
Get Shareaholic
Tags: .NET snippet

 Posted in: Microsoft.NET
September 4, 2009 | admin | No Comments

Leave a Reply

 


  • « Previous post
  • Next post »
  • Recent Posts

    • Installing PyGraphviz on Windows
    • Convert python object to XML representation
    • Liferay Portlet Development
    • Norway Road Show 2011 private meeting invitation
    • Oracle OpenWorld 2011
  • Ropardo is Hiring

  • Subscribe

    • Add to Google Reader or Homepage Add to netvibes TopOfBlogs
  • Recent Comments

    • Rajkumar Pomaji on Bluetooth PC Remote Control
    • Stelian Morariu on GWT 2.1 – Uploading a file using the RPC mechanism
    • Sergio on GWT 2.1 – Uploading a file using the RPC mechanism
    • Artem on Liferay: Deployment will start in a few seconds… and how to realy start
    • rkd80 on GWT 2.1 – Uploading a file using the RPC mechanism
  • Archives

    • November 2011 (1)
    • September 2011 (4)
    • July 2011 (3)
    • June 2011 (2)
    • May 2011 (4)
    • April 2011 (4)
    • March 2011 (3)
    • February 2011 (2)
    • January 2011 (2)
    • December 2010 (1)
    • November 2010 (4)
    • October 2010 (4)
    • August 2010 (3)
    • July 2010 (3)
    • June 2010 (6)
    • May 2010 (8)
    • April 2010 (7)
    • March 2010 (9)
    • February 2010 (6)
    • January 2010 (5)
    • December 2009 (7)
    • November 2009 (9)
    • October 2009 (10)
    • September 2009 (14)
    • August 2009 (10)
    • July 2009 (1)
    • June 2009 (1)
    • May 2009 (1)
    • April 2009 (1)
    • March 2009 (1)
    • October 2008 (3)
    • October 2007 (3)
    • July 2007 (4)
    • June 2007 (1)
    • May 2007 (3)
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Categories

    • News (15)
    • Ropardo Team (8)
    • Ropardo Products (6)
      • File Tracking Client (4)
      • iManagement (2)
    • Software Development (83)
      • Microsoft.NET (22)
      • Java (40)
      • Oracle (8)
      • Power Builder (3)
      • Liferay (5)
      • Lotus Notes (9)
      • xWiki (4)
    • System Adminstration (13)
      • Linux (10)
      • Windows (3)
    • Programming (1)
    • Uncategorized (3)
    • Databases (10)
      • MSSQL (5)
      • PostgreeSQL (3)
    • Microsoft.NET (1)
    • Web Development (28)
      • ASP/ASPX (3)
      • Content Management Systems (1)
      • HTML/CSS (5)
      • Javascrip/AJAX (8)
      • PHP (7)
    • Oracle E Business Suite (6)
  • Tags

    .NET ajax blog C# certification client CMS control css database Debugging django Domino Eclipse extension file tracking filter fun gentoo google Hibernate how to html image iManagement import Java javascript jQuery liferay Linux Lotus Notes lotus script Oracle Oracle BI Publisher 11g PHP portal PostgreSQL powerbuilder Python SQL Telerik velocity xml Xwiki

© 2010 ROPARDO s.r.l..

Powered by WordPress. Styled by Ropardo