RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Having Fun with Code Snippets : Page 2

Using code snippets can make it quick to add common code pieces to your application, while creating your own snippets allows you to build a library of custom code pieces and share them with other developers.

Managing Code Snippets
Seeing the potential productivity gains with using snippets, several questions may cross your mind: Where are these snippets stored? Can I reorganize my snippets? How do I add my own snippets? Can I find more snippets on line?

The tool that can answer all of these questions is the Code Snippets Manager. The Code Snippets Manager, available from the Tools menu, provides a summary of each snippet, its shortcut, type, author, and location as shown in Figure 4.

Figure 4: The Code Snippets Manager helps you organize your snippets.
For each language (Visual Basic, Visual C#, Visual J#, and XML), the Code Snippets Manager lists every snippet using the same folder hierarchy as the Code Snippet Picker. Select a snippet from within the folder hierarchy to see its details in the right pane.

To add a new folder to the folder hierarchy, use the Add button. To remove a folder and its contents from the folder hierarchy, use the Remove button. This allows you to reorganize your snippet folders as you desire.

To add a new snippet, navigate in the folder hierarchy to the desired folder and use the Import button to add a new snippet to that folder. This assumes, of course, that you already have a new snippet. More about this later. Use the Search Online button to search online for additional code snippets.

Using the Code Snippets Manager provides an easy way to see the code snippets that are available and to reorganize them as needed.

A Peek Inside Snippets
Let's take a peek inside a snippet to see how it is made. Then it will be an easy step to make our own snippets.

As shown in Figure 4, the Code Snippets Manager provides information on where each snippet is stored on your system. Let's take a look at the property procedure snippets used earlier in this article.

Using the Code Snippets Manager provides an easy way to see the code snippets that are available and to reorganize them as needed.
Navigate the folder hierarchy in the Code Snippets Manager to find the property procedure snippet. For Visual Basic, navigate to Common Code Patterns, choose Properties and Procedures, and then choose "Define a property." For Visual C#, use Visual C# and choose "prop." View the location of the snippet above the folder hierarchy.

Once you know the location of the snippet on your system, use Explorer to navigate to the snippet's directory and open the snippet. The property procedure snippet in Visual Basic is called DefineAProperty.snippet and is shown in Listing 1; in Visual C# it is called prop.snippet and is shown in Listing 2.

Notice that the snippet is actually XML (which should not be a surprise). The majority of the XML defines the snippet details such as its name, shortcut, and description. The key part of the XML file is the snippet itself.

In Visual Basic:

   <Code Language="VB" Kind="method decl">
   <![CDATA[Private $PrivateVariable$ As 
     Public Property $PropertyName$() As $PropertyType$
           Return $PrivateVariable$ 
        End Get
        Set(ByVal value As $PropertyType$)
           $PrivateVariable$ = value
        End Set
   End Property]]>
In C#:

   <Code Language="csharp">
   <![CDATA[private $type$ $field$;
     public $type$ $property$
        get { return $field$;}
        set { $field$ = value;}
The snippet uses the $ character to delimit the replacements in the snippet. The replacement can have any name, and using the same replacement name throughout the snippet provides for cascading edits.

You can edit the snippet files to modify the snippets. For example, if you would like to change the shortcut associated with a snippet, you can modify the snippet file Shortcut element.

Now that you know what a snippet file looks like, you can build your own snippets.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date