This is my eleventh year writing the VB Today column for codeguru.com. According to my Editor that makes me the longest running columnist (that he knows of). My day job is technical evangelist for Developer Express. What makes the job great is I love the tools and use them daily. It is easy to promote, talk, and write about products that you love. This article explains how CodeRush can help you write code faster in Microsoft Visual Studio .NET, with less effort, and be one of those hyper-productive programmers of myth and legend. (A great example of this, is that Mark Miller using an XBox 360 guitar tweaked for CodeRush in Visual Studio.NET was able to program faster than almost all challengers, including me but not Dustin Campbell.)
In this article you will learn how to use CodeRush to write your VB.NET code and how to define new templates for your own inventions.
In My Own Words...
Mark Miller is wicked smart. I can write that because it's true (and he knows it.) Mark created CodeRush and has been creatively evangelizing it on behalf of Developer Express for many years. Some of you probably saw the XBox 360/Beatles extravaganza at PDC in 2009. Some of you probably saw the Verne Troyer 'mini me' craziness at PDC LA 2008?!
In my own words CodeRush is an extensible meta- programming tool that has hundreds of templates and supports custom templates. The templates represent code. You type a simple sort of shorthand and CodeRush injects the code associated with that template into your source file. The shortcuts are usually one or two characters, but the corresponding code that expands from the shortcut can be as big as you want. CodeRush uses markers and fields to show you where you need to fill in the blanks.
You will see how this works in the next section. The final section provides an introduction into defining a custom template, and the features that support custom templates. (CodeRush is a tucked away, but it is a feature- rich tool. Too much to cover in one article, but you will have a good idea of what is available.)
Using Existing Templates to Write VB Code
CodeRush is available from http://www.devexpress.com, and can be purchased as a separate download or with one of Developer Express' subscription plans. (You also have the option of using CodeRush Xpress as a free download from http://www.devexpress.com/Products/ Visual_Studio_Add-in/CodeRushX/.) The examples in this article were created with the commercial-purchased version- of CodeRush.
Note: CodeRush Xpress does not include support for the code templates discussed in this article.
When you install CodeRush it integrates into Microsoft Visual Studio on an added DevExpress menu. The templates for VB.NET and C# are ready to go once CodeRush is installed. The DevExpress menu provides access to the tools window, training content, options and a user guide, but the defined templates generate code when you type the template shortcut followed by the space bar inside any source code file open in Visual Studio.
The first thing to note is that core templates are pretty intuitive. Type 'c' space for the class template, 'v' space for variables, 'p' space for properties, and so on. For example, 'cc' space will generate a class constructor and permit you to select fields from the class that you would like to initialize inside the constructor (Sub New). There are hundreds of templates, and it takes some time to memorize more than a handful. However, the more you use CodeRush the more muscle memory will take over and learning and remembering templates that you use frequently unfolds very quickly.
Here is a step by step example showing how, I for example, define a custom entity class using CodeRush. (The example is based on the Northwind Customers table for convenience.)
- Open the Server Explorer in Visual Studio
- Expand the Northwind connection
- Right-click the Customers table and select
Open Table Definition
- Select all of the elements in the definition and copy and paste them into a code file (See Listing 1)
- Move the cursor outside of the Module after
End Moduleand between the table definition
- Type 'c' and hit the space key
- The cursor position will be placed on the default class name,
- Quickly type in a new name-Customer-and press Enter
- Define each property very quickly by type 'p' and pressing the space key
- Use the pre-defined cursor positions to replace the field name, property name, and data type for the template, pressing enter after each revision
- After the last replaceable element is typed in the cursor will be placed at the end of the property statement. (Note that the property template generates a backing field too.)
- Repeat steps 9 and 10 for every field in the Customers table. (Since Customers' fields are all strings you can use '
ps'--the string property template--to speed things up
- Delete the field definition data information from the source file. (In just a minute or two you have a custom entity, see Listing 2)
- To generate a
constructorthat accepts all fields find an empty line in the class and type 'cc' space followed by enter to accept the default (see Listing 2)
- Cut and paste all of the class code to a separate source file (or use the context menu, hover over the class name select the Refactor! Menu item and click Move Type to File. Refactor! Pro is a refactoring tool from Developer Express, bundled with CodeRush and also available separately for $99.)