Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

September 8, 2009

Using Code Snippets

Filed under: C#,VB.NET,Visual Studio @ 1:24 pm

Are there some pieces of code that you find yourself writing over and over and over again? Do you ever use a piece of code so seldom that you always need to look it up? Some of these pieces may make sense as standard components or standard methods in an application framework, like logging or validation (see this link for an example Validation class), so you never need to write them or look them up again. Other pieces, like Property statements, loops, exception handlers, or file input/output, are more unique and cannot easily be made into standard methods. For these types of code pieces, code snippets are a perfect answer.

A code snippet is a prebuilt intelligent piece of code, sometimes referred to as an expansion template, that you can easily insert into the Code Editor. Visual Studio comes with a large library of built-in code snippets for Visual Basic and for C#. You can also build your own code snippets.

Using code snippets makes it quick to add predefined code pieces to your application. Creating your own code snippets allows you to create a library of custom code pieces and share them with other developers.

This post demonstrates how you can insert existing code snippets into your application. A later post will describe how to build your own code snippets.

Code snippets include standard programming constructs, such as loops and Property statements, to help you quickly build common code blocks. For VB, code snippets also include common programming tasks, such as creating an MDI child form, to help you quickly insert commonly used code. The VB code snippets also include many lesser-used programming techniques, such as drawing a filled rectangle, to provide assistance with tasks that you don’t use as often and would otherwise need to look up. With all of this functionality available at your fingertips, code snippets can greatly enhance your productivity.

To insert a code snippet into the Code Editor use the following steps.

In C#:

1. In the Code Editor, place the insertion point where you wish to insert the snippet.

2. Right-click and select Insert Snippet from the context menu. The Code Snippet Picker appears:

 image

3. Double-click or press the Tab key on the snippet folders to navigate
the hierarchy and find the desired snippet. To back up in the hierarchy, press the Backspace key or press Shift+Tab. To cancel the Code Snippet Picker, press the Esc key.

4. Double-click on the desired snippet. Visual Studio inserts the code associated with the selected snippet into the Code Editor at the current insertion point.

In VB:

1. In the Code Editor, place the insertion point where you wish to insert the snippet.

2. Right-click and select Insert Snippet from the context menu, or
type a question mark (?) and press the Tab key. The Code Snippet Picker appears:

 image

3. Double-click or press the Tab key on the snippet folders to navigate
the hierarchy and find the desired snippet. To back up in the hierarchy, press the Backspace key or press Shift+Tab. To cancel the Code Snippet Picker, press the Esc key.

4. Double-click on the desired snippet. Visual Studio inserts the code associated with the selected snippet into the Code Editor at the current insertion point.

Snippets also have shortcuts to minimize the steps required to access a
snippet. The shortcut associated with a snippet is displayed in the ToolTip
of the Code Snippet Picker entry.

In C#:

image

In VB:

image

To insert a code snippet using the shortcut…

In C#:

1. In the Code Editor, place the insertion point where you want to insert the snippet.

2. Type the shortcut, press the Tab key, and press the Tab key again. Visual Studio inserts the code associated with the selected snippet into the Code Editor at the current insertion point.

In VB:

1. In the Code Editor, place the insertion point where you want to insert the snippet.

2. Type the shortcut and press the Tab key. Visual Studio inserts the code associated with the selected snippet into the Code Editor at the current insertion point.

For example, when you’re building classes, one of the more tedious
tasks is defining all the properties. You have to create a private backing
variable and a property getter and setter for every property. A more efficient way to create your properties is to use the property code snippet.

In C#:

Use one of the following techniques:

  • Display the Code Snippet Picker, navigate the snippet folders and
    then double-click on the defined snippet name.
    To insert the property code snippet, select Visual C#| prop.
     
  • Use the shortcut for the code snippet by typing the shortcut name
    in the Code Editor and pressing the Tab key two times.
    To insert the property code snippet, type prop, press the Tab key, and then the press the Tab key again.
     
  • Type part of a shortcut name and use the Intellisense drop down to pick the snippet.
    To insert the property code snippet, type "pro" and Intellisense displays all snippets that begin with those letters. Press the Tab key to select the item from Intellisense, and then press the Tab key again to insert the snippet.

Regardless of the technique you used when the property snippet is inserted into the Code Editor, the following code is generated:

image

Notice that this uses the auto-implemented property syntax.

In VB:

Use one of the following techniques:

  • Display the Code Snippet Picker, navigate the snippet folders and
    then double-click on the defined snippet name.
    To insert the Define a Property code snippet, select Code Patterns – … | Properties, Procedures, Events | Define a Property.
     
  • Use the shortcut for the code snippet by typing the shortcut name
    in the Code Editor and pressing the Tab key.
    To insert the Define a Property code snippet, type property and then the press the Tab key.
     
  • Type part of a shortcut name and use the Intellisense drop down to pick the snippet.
    To insert the Define a Property code snippet, type "pro" and Intellisense displays all snippets that begin with those letters. Press the Tab key to select the item from Intellisense, and then press the Tab key again to insert the snippet.

Regardless of the technique you used when the Define a Property snippet is inserted into the Code Editor, the following code is generated:

 image

Many snippets contain highlighted text, called replacements (shown in green in the above two screen shots). A replacement is a variable or other text in the snippet that you can replace with a unique value when you insert the snippet. Modifications to a replacement cascade through the snippet, so changing any replacement automatically changes all other replacements with the same text.

Visual Studio makes it easy to modify the replacements. When Visual Studio inserts the snippet, it sets focus to the first replacement (shown in gray in the above two screen shots). You type the desired value for the replacement and press the Tab key. Visual Studio cascades your replacement value through the snippet and moves the focus to the next replacement. This process is repeated until all replacement values are entered or until you leave the snippet.

For example, to define a FirstName property…

In C#:

Type string in the first replacement (int) and press the Tab key twice. Then type FirstName in the next replacement (MyProperty) and press the Enter key and you are done. The result is as follows:

public string FirstName { get; set; }

In VB:

Type _FirstName in the first replacement (newPropertyValue) and press the Tab key. Notice how the value is cascaded to the getter and setter. Type String in the second replacement and press the Tab key (or since the default is String, just press the Tab key). Finally, type FirstName in the next replacement (NewProperty) and press the Tab key, and you are done. The result is as follows:

Private _FirstName As String
Public Property FirstName() As String
    Get
        Return _FirstName
    End Get
    Set(ByVal value As String)
        _FirstName = value
    End Set
End Property

If the snippet you insert requires a reference or import that you don’t currently have set, Visual Studio automatically adds the reference and associated import when you insert the snippet.

Inserting snippets into your code is quick and easy and saves you from all that typing. Using snippets can also save you time, because they provide prebuilt code for tasks that you may not perform very often.

(Based on an except from "Doing Objects in Visual Basic 2005".)

Enjoy!

3 Comments

  1.   Waleed El-Badry — September 10, 2009 @ 2:15 am    Reply

    Thanks Deborah for submitting this article. But what about creating custom code snippets using snippet editor. I usually code the snippet in the XML editor, however, I hope you can highlight the usage of the code snippet editor as I believe it would be much easier to work with.

    Have a lovely day

  2.   Danny — October 2, 2009 @ 5:00 pm    Reply

    Great way to manage snippets. There are more at http://www.snippetgood.com

  3.   Jayce — September 19, 2011 @ 11:43 am    Reply

    Frnalky I think that’s absolutely good stuff.

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2022 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs