Subscribe Now

ABC, 123, Ruby, C#, SAS, SQL, TDD, VB.NET, XYZ

Wednesday, October 31, 2007

Your Friends, The Visual Studio Code Snippets

There are some great and well-known third-party tools that enhance or complement Visual Studio .NET, such as NUnit and Roeder's Reflector. Visual Studio's Code Snippets, both the default (built-in) snippets, and the C# and VB.NET add on snippets are amazingly useful and seemingly under-advertised and under-utilized. I don't hear many people talking about them and I haven't seen much use of them.



What are code snippets? Code shortcuts for writing bigger chunks of code. A quick and powerful example is a good way to introduce them.



Here's the default code snippet "prop" in action in C# that codes up an entire read/write property complete with private storage variable. They way I use this is instead of writing everything out by hand to code out a property, I simply type "prop" followed by TAB, TAB. As you can see, prop even appears in Intellisense.



prop code snippet in Intellisense dropdown list

After I hit TAB the 2nd time, I see this.



prop code snippet fills in a lot of code for you

Then I just type a quick "string", TAB, TAB, "myName", TAB, "Name" and I'm done!



quick product of using the prop code snippet

Will I ever type out another private member/getter/setter property combo again? I doubt it.



The official documentation says, "The Code Snippet Inserter is invoked through the Insert Code Snippet or Surround With commands on the IntelliSense menu, or by using the keyboard shortcuts CTRL+K, then X and CTRL+K, then S respectively." I find it easiest to just type out the snippet's keyword and press TAB, TAB. Things are a little different in the VB.NET editor. Snippet keywords don't appear on Intellisense lists, but simply right-click and select Insert Snippet.



in VB.NET right-click and select Insert Snippet

After clicking Insert Snippet, a categorized menu of snippets is presented.



snippet menu

Selecting Common Code Patterns and then Exception Handling and shows the available snippets in that category.



Exception handling code snippets

Selecting Try...Catch...Finally...End Try Statement inserts this code.



Try...Catch...Finally...End Try code snippet

Now I just need to decide what class of Exception I want to catch and I'm off and running.



To add on more snippets from MSDN or make and add your own, use the Code Snippets Manager.



Code Snippets Manager

For example, adding the Database code snippets gives you nice shortcuts such as "adoCreateSqlConn" which generates the following code.



System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString = @"Data Source=ServerName;Initial Catalog=Northwind;Persist Security Info=True;User ID=;Password=";


I hope this was helpful. Enjoy!

1 comment:

Anonymous said...

In the VB.NET editor, the code snippet menu can also be brought up by pressing TAB-? (i.e., press and hold the SHIFT, /, and TAB keys in quick succession).