Code snippets were introduced in the Visual Basic 2005 release. They provide an easy way to learn how to do a particular coding task, or to re-use a piece of code in various parts of your application. Code snippets can be inserted in a variety of ways. The method you choose will depend upon the situation. When browsing for a code snippet, use the Code Snippet Inserter:
Code Snippet Inserter
The Code Snippet Inserter can be invoked in the following two ways: by typing ‘?+Tab’ in your code file, or by right-clicking and selecting “Insert Snippet…” from the context menu that appears. You can then navigate the snippet directory structure and select the snippet you want to use. Notice that tooltips appear beside the titles to give the description and shortcut of the currently selected snippet:
For snippets that you use more frequently or for which you want quicker access, you should use the snippet’s shortcut. Once you know the snippet’s shortcut, you can just type the shortcut+Tab in code to insert the snippet. To learn a snippet’s shortcut, either select it in the Code Snippet Inserter (as pictured above) or view it in the Code Snippets Manager. The Code Snippets Manager can be accessed via the Tools menu, and is pictured below. It displays a number of relevant fields of the snippet:
Code Snippets Manager
The ‘shortcut+Tab’ insertion method also gives a neat effect for snippets which use keywords as their shortcuts. For example, type ‘Select+Tab’ in your code, and you’ll see the following code snippet inserted. These kind of snippets function as expansions.
An Expansion Snippet: Select Case
If you ever press Tab and end up inserting a snippet that you don’t want, just type Ctrl+Z and your code will return to its previous state.
For other, more task-oriented snippets, the snippet shortcuts start with an abbreviation for the corresponding snippet directory. Thus, the shortcut for the “Send an Email” snippet is ‘conEmail’, since the snippet lives in the ‘connectivity’ directory. Note: although most shortcuts include capitalized letters, the casing that you type does not have to match in order for the snippet to insert correctly. That is, you could also type ‘conemail’ or ‘CONEMAIL’, and the snippet would insert just fine.
Often, you may only remember a part of the snippet’s shortcut. In that case, you can type the beginning of the shortcut, press ?+Tab, and a shortcut completion list will appear. For example, to insert a snippet from the Application snippet directory which uses ‘app’ as its shortcut prefix, you could type ‘app’+?+Tab or ‘a’+?+Tab in order to invoke the list. The shortcut list appears and the title of the currently selected snippet displays in the tooltip. You can then double-click or press Enter to insert the snippet into your code.
Snippet Shortcut List
Most of the methods we’ve discussed for inserting a snippet into your code involved some sort of inline typing. However there is still one more way to insert a snippet, which has a different feel than the others. And that is: drag-and-drop. In their essence, code snippets are xml files with a .snippet extension, which get copied to your computer when you install Visual Basic. The Code Snippets Manager displays the location of the snippet files, so that you can find them on disk. Once you determine that path and locate your snippet files on disk using Windows Explorer, you can simply drag and drop a snippet directly into your code!
Try out all these different techniques, and decide which you like best!