Write Code Without Having to Type It

A lot of the code that developers write follows common patterns. For example, when you create a new property, you follow four steps:

  1. Declare a private variable.
  2. Declare the public property with a public name of the same type.
  3. Declare the Set logic, which sets the private variable to the parameter value.
  4. Declare the Get logic, which returns the private variable's value.

These four steps are common to every property you want to place in a class. Manually typing all that information is not only repetitive but also time consuming and error prone (mainly typing errors).

Visual Studio 2005 lets you avoid all that hassle. Its new code snippet feature lets you include code without typing it. For example, to include code that creates a property, you simply type "prop" if you're using Visual C# or "property" if you're using Visual Basic (VB) 2005, then press the Tab key. In your source code, you'll find a template that includes all the generic code you would normally type. The sections that you need to edit in your new property declaration (e.g., variable name, property name, type) are all highlighted. After you edit the contents of a highlighted section, you press the Tab key to go to the next section. The changes are reflected across the entire declaration for consistency. This constitutes an important difference between using this feature and uploading stock code. With this feature, you're literally pulling in a code template with active tags that specify the initial customizable properties in that template.

Most all the keywords in VB 2005 or Visual C# have been associated with a code snippet. Can't remember the exact syntax of the For loop? No worries. Type "For", press the Tab key, and--poof--the necessary code appears. Can't remember the keyword you need to use? No problem. Right-click somewhere in your edit window and select Insert Snippet. Visual Studio 2005 first inserts a placeholder in the location where you want to include a snippet, then provides a list of snippet categories in the Code Snippet Picker.

Visual Studio 2005's code snippet feature goes well beyond just providing templates for common language elements. Templates are also provided for common tasks, such as pinging a remote computer or connecting to a database. In addition, a template contains more than just source code. The template also includes additional information, such as the snippet's author, the namespaces that need to be imported (if applicable), a display title, keywords the user needs to edit, and which keyword should be edited first. For general information about code snippets, you can check out the Visual C# help files at http://msdn2.microsoft.com/en-US/library/f7d3wz0k.aspx.

The good news is that Visual Studio 2005 shipped with hundreds of code snippets. The bad news is that the snippets are, as you might expect, language specific and therefore not interchangeable. For example, some of the VB 2005 snippets leverage the "My" namespace, which doesn't exist in Visual C#.

The code snippets have their own schema, which is documented at http://msdn2.microsoft.com/en-US/library/ms171418(VS.80).aspx. As a result, you can create your own custom snippets. Step by step instructions for doing so are available at http://msdn2.microsoft.com/en-US/library/ms165392(VS.80).aspx.

Snippet files are XML files. So, to create snippets, you must edit an XML file, which isn't much fun. Fortunately, if you use VB 2005, there's a GUI called the Visual Basic Snippet Editor that you can use. This free editor is currently in the release candidate (RC) stage. For more information, visit the Microsoft Developer Network (MSDN) Web page at http://msdn.microsoft.com/vbasic/downloads/tools/snippeteditor. Or for the most recent version, bypass MSDN and look in the GotDotNet Workspace at http://www.gotdotnet.com/Workspaces/Workspace.aspx?id=a927f4e7-8e7f-45ce-8b72-f3b9384a3eab.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.