|
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 <Tab> key. This screen was captured right before I pressed <Tab>.
The same screen a second later after I pressed <Tab>.
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 <Tab> key. It sounds confusing, but after you've done it a few
times it becomes second nature. That'll bring up a list of all the registered shortcuts.
From there you can scroll to the one you want and select it from the list.
The other time saver that you may find useful (especially until you learn some shortcuts)
is the <Ctrl-K>, <Ctrl-X> key combination. It'll bring up the
"Insert Snippet" browser without you needing to right-click to bring up the
context menu or go to the "Edit -> IntelliSense -> Insert Snippet..." menu.
The Code Snippets Manager
The next step in dealing with snippets is the "Code Snippets Manager". You can
get there by going to "Tools -> Code Snippets Manager..." or
by using the <Ctrl-K>, <Ctrl-B> key combination.
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.
Conclusion
I hope this article has helped you see just how simple and powerful Visual Studio's Code
Snippets are. Next time I'll cover downloading and installing additional snippets as well as
creating your own both manually and by using the Code Snippet Editor.
|