Recipe 2.1. Discovering and Using a Code Snippet


Problem

You know that Visual Studio came with a bunch of prewritten "snippets" that you can use in your applications, but you don't know where to find them in the vast Visual Studio menu system.

Solution

Code snippets are among the IntelliSense features included with Visual Studio. To find and insert a snippet, use the different snippet-related menus and keyboard sequences.

Discussion

To insert a code snippet into your source code, right-click at the desired location with the mouse, choose Insert Snippet from the shortcut menu (Figure 2-1), and navigate to the snippet you want to use.

Figure 2-1. Inserting a snippet with the mouse


An even faster method is to type a question mark ( ?) anywhere in the source code and then press the Tab key. The more formal location of this same command within the Visual Studio menu system is at Edit IntelliSense Insert Snippet. If you are in any way mouse-phobic when developing source code, you can use the default Visual Basic keyboard shortcut of Control-K followed by Control-X to get to the snippet picker.

Using any of these methods to access snippets presents the top-level set of snippet folders, as shown in Figure 2-2.

Figure 2-2. Primary snippet categories


To negotiate the hierarchy, use the mouse or arrow keys to select a folder or item in the pop-up list, or type a partial list name followed by the Enter key. Selecting a snip-pet folder updates the list with the items and subfolders in that selected folder. For example, in Figure 2-2, selecting "Math" with the mouse or typing "Math" from the keyboard followed by the Enter key, will open the "Math" snippet folder and display any folders or items contained within that folder. Selecting an item inserts the chosen snippet.

Each snippet contains a useful block of prewritten code, but many also include some intelligence. Some snippets include "fill in the blank" templates that provide areas for you to supply your custom values. For instance, the Data Typesdefined by Visual Basic Convert a Number to a Hexadecimal String snippet includes a field for the source value, moving the insertion point to that field immediately upon pasting the snippet in the code:

 Dim hexString As String = Hex(48) 

Some snippets place multiple lines of source code in the code editor, sometimes with multiple replacement fields. The Common Code Patterns Types Define a Structure snippet defines this multiline structure:

 Structure MyStructure    Public ValueOne As Integer    Public ValueTwo As Boolean End Structure 

Some snippets add code to various places in your source-code file and may make other updates to your project. The Security Encrypt a String snippet not only adds code to the active procedure but also adds Imports statements to the top of the source-code file if references to the namespaces it uses are not already there.

Snippets are somewhat location-dependent. Most are written to be used inside a sub-routine, function, or property accessor, while a few are designed for placement out-side of routines or classes. If you insert a snippet at the top of a source-code file, outside of any class context, it will be riddled with errors.

Snippets are actually specially formatted XML files, with attributes containing the special insertion rules for each snippet.

See Also

Recipes 2.2, 2.3, and 2.4 provide additional information on code snippets.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net