Snippets are important to learn because they will save you time. A snippet is a set of
keystrokes that form a template for a piece of code. The code for a snippet is typically
something that is common in normal programming. You'll see many common statements
and blocks of code in this chapter, many of which have associated snippets. This section
shows you the mechanics of using snippets, and you'll see more examples throughout the
rest of this chapter.
To use a snippet, begin typing the snippet prefix until the snippet acronym appears in
the Intellisense completion list, press the TAB key twice, and fill in the snippet form while
tabbing through each field. Press ENTER when you're done.
Since you've already learned about namespaces, I'll show you the namespace snippet.
To start, open any code file and click to start typing in a part of the file outside of all code
blocks, such as directly below any using statements but above any existing namespace
statements. Type the letter n and watch the completion list go straight to the namespace
element. Type an a and you'll see the namespace alone in the completion list, as shown
in Figure 2-7.
NOTE
The CTRL-ALT-SPACE keystroke in Figure 2-7 switches between the Intellisense modes
Consume First and Standard mode. In Standard mode, which shows CTRL-ALT-SPACE,
typing characters automatically selects keywords. However, there are situations where
you are trying to type a word that doesn't exist yet and Intellisense is too aggressive by
adding the selected completion list item, instead of what you typed. In those cases, you
can press the CTRL-ALT-SPACE keys to go to Consume First mode and what you've typed will
be selected. You can still use the DOWN ARROW key on your keyboard in Consume First
mode to select the highlighted term in the completion list.
Figure 2-7 Using snippets
Figure 2-8 Filling in the Snippet template
You can identify snippets in the completion list by the torn paper icon. At this point,
you can press the TAB key to complete the namespace keyword. Then press TAB again to
produce a template where you can fill out the highlighted fields. Figure 2-8 shows the
results of creating a namespace snippet by typing n and pressing TAB, TAB.
As shown in Figure 2-8, you would type in the Namespace name in the highlighted
form field to replace MyNamespace, which is placeholder text. For templates with more
fields, you would press the TAB key to move between fields. In the case of the namespace
shown in Figure 2-8, there is only one field in the template to complete.
VB offers a couple of ways to add snippets: by typing prefixes or via a pick list. To see
how VB snippets work, place your carat inside of the Module1 module, underneath End
Main (not inside of the Main block). Type Su and press TAB, and notice that VS creates a
Sub (method) along with a template containing a field for filling out the Sub snippet.
Another way to add VB snippets is to type a ? and press TAB. You'll receive a pick list,
as shown in Figure 2-9. You can navigate this pick list to find the snippet you need, as
classified in one of the folders. VB ships with many more built-in snippets than for C#.
Now that you know how to use snippets, let's move on to the different types of
statements you can have in C# and VB and how snippets work with those statements.
Figure 2-9 VB snippet pick list
No comments :
Post a Comment