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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Having Fun with Code Snippets : Page 3

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.




Application Security Testing: An Integral Part of DevOps

Building Your Own Snippet
To get the greatest benefit from snippets, you will want to create your own. There are several ways you can approach this task.

You can build your own snippets the hard way by manually creating the snippet file contents yourself with your XML editor of choice. But this means that you have to have an intimate understanding of the schema of the XML and have to do a lot of typing, typing, typing.

If you use the XML code window provided in Visual Studio 2005, you will get some assistance with creating your snippet.
If you use the XML code window provided in Visual Studio 2005, you will get some assistance with creating your snippet. The XML code window Code Snippet Picker provides a Snippet snippet. This snippet provides you with the correct XML layout for a snippet XML file. All you need to do is tab through it to set the appropriate element values and then add the code you want for the snippet itself.

Another choice is to locate a snippet file that is similar to the snippet that you want to create. Copy the file, then edit it as you desire. This is the best choice if you simply want an enhanced version of one of the existing snippets.

By far the easiest way to create your own snippets is to use the open source Snippet Editor created by Bill McCarthy (Check out his blog), a developer from Australia and Microsoft MVP, in conjunction with Microsoft.

After downloading the Snippet Editor, launch the file SnippetEditor.exe. By default, the Snippet Editor displays Visual Basic snippets in a folder hierarchy in the left pane. Note that this is the same folder hierarchy as displayed in the Code Snippets Manager except that the snippet name is displayed instead of the title. To add snippets for other languages, use the Option button (on the far right of the toolbar) and select the desired language(s).

The Snippet Editor dialog allows you to edit existing snippets or create new ones. It allows you to add snippet folders and drag and drop snippets between the folders. It also allows you to build a Visual Studio Content Installer, making it easier to share your snippets with others. Most importantly, it allows you to search snippets by keyword using the filter feature just above the folder hierarchy.

For example, say you want to find any snippet having to do with properties. Type "prop" in the filter text box, click on the Apply button, and the folder hierarchy in the left pane of the Snippet Editor is filtered to only those snippets with the letters "prop."

Say you want to update the VB property snippet so that the private member variable is always prefixed with an underscore. Navigate the filtered folder hierarchy to the DefineAProperty snippet. Double-click on the snippet to edit it. It will then appear in the Snippet Editor as shown in Figure 5.

Figure 5: Create your own Visual Basic snippets using the VB Snippet Editor. Note that this is an open source project and the user interface may change.
The Editor tab in the top right pane of the Snippet Editor allows you to edit the snippet as desired. The Preview tab provides a preview of what the code expansion will look like so you can confirm your expected results.

The bottom right pane of the Snippet Editor provides tabs to assist you with the editing of your snippet. The Properties tab displays the properties of the snippet such as the title, description, and shortcut.

The Replacement tab allows you to define the fields that are to be used as replacements when the snippet is expanded. You can add new replacements, modify existing replacements, or delete a replacement.

The References tab defines which references need to be added when the snippet is inserted into the code window. Similarly, the Import tab defines which imports need to be set.

Create your own snippet by right-clicking on the desired folder and selecting Add New Snippet from the context menu.

For example, say you want to create a specialized property procedure that knows how to handle object instances. Create your own snippet for this under common code patterns, choose properties and procedures, and name it InstanceProperty.

Double-click on the new InstanceProperty snippet to edit it. On the Properties tab, give the snippet a title, description, shortcut, and so on.

On the Replacements tab, add a replacement for the data type, private variable name, and property name. Then write the desired code in the Editor pane (or copy from one of the other snippets and edit it), using the replacements as needed.

Private $PrivateVariable$ As $PropertyType$ Public Property $PropertyName$() As $PropertyType$ Get If $PrivateVariable$ Is Nothing Then $PrivateVariable$ = New $PropertyType$ End If Return $PrivateVariable$ End Get Set(ByVal value As $PropertyType$) $PrivateVariable$ = value End Set End Property

Click on the Preview tab to confirm the results. When you obtain the desired results, save the snippet. The snippet is automatically added to the Code Snippet Manager in the defined folder. So it is immediately ready for your use in the code window.

Any time you find yourself typing the same set of code or looking up some syntax that you don't use very often, consider building a snippet for that code. As you build up your snippet library, you will have the code for many of your coding tasks right at your fingertips.

Try Out Snippets
Using snippets can provide a huge time savings, both in minimizing typing and in looking up syntax for commonly needed tasks.

Your snippets are yours. Modify them, reorganize them, and create your own so you can build a very efficient, personalized snippets library. And have fun!

Deborah Kurata is cofounder of InStep Technologies Inc., a professional consulting firm that focuses on turning your business vision into reality using Microsoft .NET technologies. She has over 15 years of experience in architecting, designing, and developing successful .NET applications. Deborah is the author of several books, including "Doing Objects in Visual Basic 6.0" (SAMS) and "Doing Web Development: Client-Side Techniques" (APress). She is on the INETA Speaker's Bureau and is a well-known speaker at technical conferences.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



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