| By Tommy Newcomb | Article Rating: |
|
| November 17, 2006 02:45 PM EST | Reads: |
15,493 |
Notice that the Declarations element contains the Literal and Object elements. These are what make automatic code replacement possible. The Literal element identifies a replacement placeholder for code that's contained entirely within the snippet. Examples of literals might be strings, integers, or variable names. The Object element identifies a placeholder, required by the snippet, which is likely to be outside the snippet itself. ASP.NET controls, Windows Forms controls, or an instance of an object or type make good candidates for this kind of element declaration.
There's also an optional element called Function that's only supported in Visual C# and Visual J# the details of which are beyond the scope of this article. However, I'll mention that there are only four built-in functions (you can't create your own) that essentially specify the function that executes when the literal or object receives focus in your code.
And finally, we come to the code. The code element defines the actual code and replacement items that will be inserted. Notice the Select...Case statement in Listing 2. The code element contains the text that's inserted within the CDATA brackets. The $ acts as a delimiter for the replacement literal and object. The attributes for the Code element are:
- Kind - Specifies the type of code the snippet has and where the snippet can be inserted. (See http://msdn2.microsoft.com/en-us/library/ms171421.aspx for more information).
- Delimiter - The value that delimits replacement variables. The default is: $.
- Language - Values must be: VB, CSharp, VJSharp, or XML
Let's look at the C# snippet in Listing 3 that I created using Notepad. Since C# doesn't have an IsNumeric function, unlike VB, I created one using this snippet. It will be the beginning of a snippet library that I'll keep for future use. The file is named IsNumeric.snippet.
The two Literal values in Listing 3 demonstrate the use of replacements after the code has been inserted. The replacements in the comments section are highlighted after the insert is executed. The developer then has to change the class name and the developer's values. (Figure 4)
Making Your Snippet Available for Use
Now that
we've created an XML snippet file, we need to make it available to the
snippet picker using the Code Snippet Manager tool in VS 2005. In the
VS IDE, select Tools | Code Snippet Manager. Here you can browse the
snippet folders and gain information about the snippets you've
installed.
When you select a snippet from your list, you'll see its description, shortcut, snippettype, and author information. You'll only see snippets here because the manager filters to only show snippet files.
The Add button should actually be renamed Add Folder, because that's what it really does. It lets you select and add a folder containing snippets, not individual snippets themselves. It should be noted that when you have a folder selected in the manager and you add a new folder it won't put the new folder underneath your selection as you might expect. Instead it will be added directly to the root.
The Import button is what you use to add your individual snippets. If you choose Import, and select your snippet, a new dialog will open up and let you place it in your directory structure. I used the Import button to add our new IsNumeric.snippet. We can see it under the My Code Snippets directory in the Code Snippet Manager. (Listing 1)
The remove button removes the folder from the manager; it doesn't, however, remove it from your system. Also note that the snippets you see in the manager are the ones you'll be able to insert using the Intellisense Snippet Picker.
And finally, the Search Online button takes you to an MSDN search text box where you can search for snippets by keyword. However, I've had more success using Google to find snippets instead.
Other Tools
If you're interested in creating your
own snippets, and you want to use a tool with more power than Notepad,
check out the Visual Basic Snippet Editor tool located at http://msdn.microsoft.com/vbasic/downloads/tools/snippeteditor/.
This is a shared source initiative project that's currently in RC mode.
This tool is essentially a UI application that lets you create
snippets, add and modify parameters like title, author, and
description, but in a more user-friendly GUI way. It also lets you
preview your snippet as it will appear in the IDE and validates for any
compilation errors that may exist. A similar tool doesn't exist yet for
C#. Now, there's a project that's screaming to be developed.
Security Considerations
Security considerations
are an important issue since snippets contain both source code and
hyperlinks. You needn't worry about snippets that come packaged in VS -
unless they're modified of course. However, downloaded snippets have
potential problems. For one, a file with a .snippet suffix doesn't
necessarily mean that it's plain text XML. These files should be
scanned with virus protection just like any other downloaded file.
The snippet code itself could be malicious. Fortunately, it's source code and you can read it, but if you are unsure of its origin, look it over carefully. Another problem that may arise is malicious code lurking in collapsed Region tags. You insert the snippet, run it, and your system is damaged because of code you didn't see.
As we discussed above, snippets may contain references that are automatically added to your project when the snippet is inserted. This may reference a file that was downloaded alongside you snippet. This could cause damage as well.
Finally, the HelpURL link in the snippet could execute a malicious script on a separate site, or link the user to an offensive site. It's wise to read the snippet's XML before installing it.
Building a Snippet Library
I hope that I've given
you direction in building your own personal library, or perhaps,
beginning a library for your company or organization. Having a
personalized snippet library helps decrease development time and
maintain coding standards. My aim is to get you started; you can decide
how a snippet library can help you and your organization.
Published November 17, 2006 Reads 15,493
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Tommy Newcomb
Tommy Newcomb works for Magenic as an IT consultant in the Chicago area.
His main focus is developing Web application and E-commerce work using Microsoft technologies. He lives with his wife, Emily, and baby daughter, Jaqueline, in the Chicago suburbs.
- Kindle 2 vs Nook
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Windows 7 – Microsoft’s First Step to the Cloud
- Cloud Expo and the End of Tech Recession
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Reality Check at the Cloud Computing Expo
- Visual Studio 2010 Is Cloud Friendly
- Fired SCO CEO Fires Back
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Cloud Computing Best Practices
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Eval JavaScript in a Global Context
- Windows 7 – Microsoft’s First Step to the Cloud
- Google Maps and ASP.NET
- Crystal Reports XI & How It Has Changed
- Converting VB6 to VB.NET, Part I
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Where Are RIA Technologies Headed in 2008?
- How to Write High-Performance C# Code
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Implementing Tab Navigation with ASP.NET 2.0
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Viewpoint: "SOA Sucks"
- Programmatically Posting Data to ASP .NET Web Applications




























