Sometimes it's the little things that can really make your day. That's
the way I feel
about Visual Studio's Code Snippets. Ever since they were introduced in
Visual Studio
2005, I've wondered how I ever wrote code without them.
Note: More so then most things in Visual Studio, code
snippets behave slightly
differently between languages. Since most of our readers code in VB,
I'll
be using it for the code examples and screen captures throughout the
article. If you prefer C#, not to worry. The same concepts
apply, but you may run across a few things which function slightly
differently
then illustrated.
What Are Code Snippets?
Code snippets are exactly what they sound like... little pieces of code
that you can
use to help you build your pages faster. They're better then
cutting-and-pasting from
a library of code because they include dynamic placeholders for the
sections of code
that you'll need to edit. Since the functionality is built right
into the Visual Studio IDE, they're also much faster then the
cut-and-paste approach
(especially once you learn the shortcuts).
Inserting a Code Snippet
Let's examine the process of inserting a basic code snippet. To begin
the process,
simply right-click where you want the code to appear in the code window.
This will bring up the context menu. From the menu, select "Insert
Snippet..."
This will bring up the Insert Snippet box. From here you can drill down
into the different snippet categories and select the snippet you want to
insert.
In this case, I'm going to chose the simple "If... Else... End If
Statement"
snippet. Notice that each snippet will also you its "Shortcut".
A shortcut lets you invoke a snippet without needing to go through the
whole process
I'm outlining here. We'll discuss shortcuts more in the next section. I
just wanted to
show you one way you can find a specific snippet's shortcut.
Once you select a snippet, Visual Studio will insert the corresponding
block of code
into the code window. The block of code will usually contain at least
one
section that you'll normally need to edit. These sections are
highlighted in green
and if you hover over them with the cursor, you'll get a tooltip telling
you what
type of expression should go there.
While the code for the snippet I picked was relatively short and simple,
there's
no reason it has to be short. The code for some of the snippets is
quite long.
For example, the snippet for drawing a pie chart in a Windows Form
application contains two subroutines.
I mention this so that you realize you're free to do as much or as
little as you'd
like in a snippet.
Shortcuts
While what I just showed you is definitely cool, if you had to go
through the whole snippet folder
process every time, it'd get old pretty fast. Not to mention it would
slow you down to the point
where the snippets probably wouldn't save you very much time at all.
Luckily Visual Studio contains
a number of shortcuts that make using snippets much quicker. The first
one is actually called a
"Shortcut", and most snippets have one.
A "Shortcut" is a short keyword that you can type in to the code window
in order to
insert the corresponding snippet. If you didn't notice, the "Shortcut"
for the
"If... Else... End If Statement" snippet I used earlier is "IfElse".
To use the "Shortcut", all you need to do is type "IfElse" and then
press
the
The same screen a second later after I pressed
What if you're not sure of a "Shortcut"? Well that's no problem either.
Simply
type the starting portion of the shortcut (at least one letter), follow
it with a question
mark ("?") and then the
The other time saver that you may find useful (especially until you
learn some shortcuts)
is the The Code Snippets Manager
The next step in dealing with snippets is the "Code Snippets Manager".
You can
get there by going to "
The "Code Snippets Manager" lets you browse the available snippets and
read their descriptions.
It'll also tell you where a particular snippet's file is located and
allows you to tell Visual Studio
which folders it should watch for snippet files.
We'll talk more about "Code Snippets Manager" in the next installment.
For now
I just wanted to let you know it's there and that you can use it to
browse through the
installed snippets.
What Does a .snippet File Look Like?
Before we start installing more snippets or creating our own, it's a
good idea to have a basic
understanding of the .snippet file format.
If you played around with the Code Snippets Manager that we discussed at
the end of the
last article, you may have noticed that when you select a snippet, it'll
show you the filename
which contains the snippet's declaration.
Since I used the "If...Else...End If" snippet for
illustration last time, let's take a look at the actual .snippet file
that makes it work.
I've been using Visual Web Developer 2008 Express for this article and
it stores the
file in question at:
IfElseEndIfStatement.snippet:
The file is split into two main sections: "Header" and "Snippet". The
"Header" section provides the basic information about the snippet. This
is where you put the
snippet's title, description, shortcut key, and other stuff like that.
The "Snippet"
section contains the actual code for the snippet as well as defining
"Declarations"
which are the editable areas which get highlighted in green when the
user inserts a snippet.
Downloading and Installing Additional Code Snippets
I was actually relatively surprised that I didn't find more sources for
snippets online.
Everyone says they have code snippets, but very few actually have code
available in
Visual Studio's snippet format. One site that actually does is
GotCodeSnippets.NET.
The thing that's a little weird about downloading snippets is that they
are usually packaged
in the Visual Studio Community Content Installer (VSI) format. Simply
save the .vsi file
somewhere you'll be able to find it. Once you've downloaded it, simply
double-click
the file and you'll be greeted with a screen that looks something like
this:
After you tell it where to install the snippet
(I recommend you install all your downloaded and custom snippets in the
"My Code Snippets" folder),
it will copy the files to the appropriate places and display a status
screen:
That's all there is to it. The downloaded snippet(s) are now installed
and can be used
just like any of the ones that shipped with your version of Visual
Studio.
Creating Your Own Snippets
Sure Visual Studio includes lots of great snippets, and sure you can
download lots more from
the Web, but the real power of snippets is unleashed when you start
creating your own. After
all, only you know how you like to work and what you spend most of your
time doing.
Manually
The format of .snippet files is simple enough that you can easily create
your own
snippets quite quickly by hand. Not wanting to type everything from
scratch, I
simply opened up an existing snippet and replaced the values with ones
appropriate
for what I wanted my snippet to do.
For the sake of illustration, I've chosen to share one of the snippets
that
I find myself using quite a bit. It's short and I intentionally gave it
a
shortcut name that is the same as the IsPostBack property of the page
object.
I did it not only to show that you can, but also because it makes the
shortcut almost impossible to forget.
This snippet simply creates a conditional that checks to be sure the
page is NOT
being posted back. I quite often write this conditional so that code to
initialize values and set the default state of objects on the page only
runs
when the page is first called. It's pretty simple so I'll just show you
the
complete .snippet file.
IsNotPostBackCheck.snippet:
Once you've completed your changes to your new .snippet file all you
need to do is place it
into the "My Code Snippets" folder located under the Visual Studio
folder in
"My Documents". The full path should be something like:
Here are a couple screen caps of the snippet in use.
Code Snippet Editor
If you're more of a GUI type of person, there are also some tools
floating around to help you
create snippets without having to write the XML yourself. The Code
Snippet Editor for Visual Basic 2008
is one such tool. You can find out more about it and download a copy
from here:
http://msdn.microsoft.com/en-us/vbasic/bb973770.aspx
The interface is quite straight-forward and you shouldn't have any
problems finding
your way around the tool. So you can get an idea what it looks like,
here's a
screen shot of the tool with the "IsNotPostBack Check" sample snippet
created
loaded for editing.
If you have any trouble with the way the tool manages snippets, don't
forget you can always use the
import functionality of Visual Studio's Code Snippets Manager or simply
copy your .snippet file to the
"My Code Snippets" folder located in your "My Documents" folder just
like we did
with the one we created by hand.
Conclusion
The first part of this article introduced Visual Studio code snippets
and showed how they can save
you lots of time and typing by reducing the need for you to manually
type repetitive code. It also
showed you how to use shortcuts to make using snippets even faster.
This part showed how you
can take the time savings to a whole new level by downloading and
creating your own snippets.
Snippets are one of the greatest time savers to come along in a long
while. If you haven't already
been using them I hope this article has helped open your eyes. If you
have, hopefully you still
learned something new and useful about these powerful little coders.
|