Useful Development Tools
The VS IDE ships with a large number of useful development tools. Although it is beyond the scope of this chapter to discuss them all, we cover some of the most important tools in this section.
The Code Region feature allows us to expand and collapse different sections, or regions, in our code modules. We can use this feature to create logical groups of methods that expand and collapse together. We can then collapse all regions in a code module that are unrelated to the one we are working with.
To create a region, we enter #Region followed by the name of the region in double quotes on a blank line above where the region should start. We then move to the next blank line below the code we want included in the region and enter #End Region (or select it from the IntelliSense list when we are prompted). Listing 24-24 shows an example of a code region.
Listing 24-24. A Code Region
#Region "Export data to Excel" 'Many lines of code here #End Region
The Code Snippets Manager (Ctrl+K Ctrl+B)
Code snippets are small, reusable pieces of code. They are stored in a snippet library and managed using the Code Snippets Manager. The VS IDE includes a large number of code snippets already written and stored in the Code Snippets Manager. Code snippets are particularly easy to use because they are exposed as part of the VS IDE IntelliSense feature. Code snippets are stored in text files in XML format. This makes it easy to use them on other computers as well as to share them with other developers. You can insert a code snippet into your code module in the following manner:
- Place the cursor at the position where you want to insert the code snippet.
- Right-click and select Insert Snippet... from the shortcut menu.
- Select the desired category.
- Select the desired code snippet.
Figure 24-19 shows the Insert Snippet command in action.
Figure 24-19 Inserting a code snippet
Instead of using the menu to insert code snippets, we can use code shortcuts. First we need to find out which code shortcuts are available in the Code Snippets Manager. The Code Snippets Manager can be accessed from the Tools > Code Snippets Manager... menu. Next we type the shortcut text, for instance ForEach, in the code editor and press the Tab key to execute it. Listing 24-25 first shows the shortcut text and then the result after we press the Tab key.
Listing 24-25. Using a Shortcut to Insert a Code Snippet
'The shortcut. ForEach 'The result. For Each Item As String In CollectionObject Next Item
As we can see in Listing 24-25, we need to fix the code snippet before it can be properly used. On first consideration it may seem like too much effort to remember all the shortcuts as well as correct the code that is actually inserted into the code editor. However, the code snippets are completely customizable, so it is worth your effort to spend some time and make the changes required to suit your needs.
The built-in Code Snippets tool is rather primitive and doesn't provide a very user-friendly interface. If you find yourself working extensively with code snippets the free Snippet Editor may be a better tool. As of this writing, it is available at www.codeplex.com/SnippetEditor.
Insert File as Text
Insert File as Text is not a standalone tool but rather a built-in function of the VS IDE. It can be used to import code from plain text files. To display the Insert File dialog select Edit > Insert File as Text... from the menu. The default file extension is *.vb so we need to change the file extension to *.txt before we can select a text file. The code in the selected text file is imported into the active code module at the current cursor position. Using text files to manage complete and reusable class modules, standard modules, and methods requires only a simple text editor, making it a portable, light-weight solution.
Task List (Ctrl+Alt+K)
The Task List is a simple but handy tool for managing the To-Do list for a solution. Using the only button on its toolbar we can create different tasks and set flags indicating their priority. By right-clicking on the list we can also sort, copy, and delete tasks.