17.1. Code Snippets Revealed

Code snippets have been around in a variety of forms for a long time but generally required third-party add-ins for languages such as Visual Basic 6 and the early versions of Visual Studio. Visual Studio 2008 includes a full-fledged code snippet feature that not only includes blocks of code, but also allows multiple sections of code to be inserted in different locations within the module. In addition, replacement variables can be defined that make it easy to customize the generated snippet.

17.1.1. Original Code Snippets

The original code snippets from previous versions of Visual Studio were simple at best. These snippets can be used to store a block of plain text that can be inserted into a code module when desired. The process to create and use them is simple as well: select a section of code and drag it over to the Toolbox. This creates an entry for it in the Toolbox with a default name equal to the first line of the code. You can rename and arrange these entries like any other element in the Toolbox. To insert the snippet you simply drag the code to the desired location in the "Code view" as shown in Figure 17-1. Alternatively, positioning the cursor where you want the snippet to be inserted, holding Shift, and clicking the snippet will place the code at the cursor location.

Figure 17.1. Figure 17-1

Many presenters used this simple technology ...

Get Professional Visual Studio® 2008 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.