picture is worth a thousand words, as the saying goes. The new Visual Studio 2005 (Whidbey) Class Designer provides a visual design environment that allows you to visualize and manipulate your classes. Being able to see your classes and work with them using a visual designer can significantly increase your productivity.
Using the Class Designer
You can use the new Visual Studio 2005 Class Designer feature to visualize your code. You can also use it to create new classes, update existing classes, or easily refactor your application.
If you have access to a Community Technology Preview (I used the May 2004 edition), you can work through the examples in this article and try out the Class Designer. If not, just follow along with the examples for an overview of the Class Designer's features.
Visual Studio provides the Class Designer as a project item. To create a class designer, right-click a project and select Class Diagram from the Add New Item dialog box. Then specify the name of the diagram. Visual Studio creates the item with the defined name and a .cd extension.
When you have a Class Designer open, the toolbox displays the items that you can insert onto the designer as shown in Figure 1. Drag an item from the toolbox to the design surface to add the item to the designer.
|Figure 1: The Class Designer toolbox provides the symbols that you use to build your class diagram.|
There are several different types of items that you can add to the designer including a class, structure, interface, and enumeration. You can also add relationships and comments.
As an example, let's create the class diagram for an application that processes orders. To keep the diagrams simple for this article, the application will have a Customer class, an Order class, and an OrderDetail class.
To show that this feature works with multiple languages, I'll create the Order and OrderDetail classes using VB and I'll create the Customer class using C#.
Begin by creating a VB project. Then add a Class Designer project item using the Add New Item dialog box and name it "Order." Visual Studio displays an empty design surface for the class diagram. Drag the Class item from the toolbox and drop it onto the design surface. The New Class dialog box prompts you to enter the class name, file name, and desired class accessibility (public, private, and so on). For the Order class, name the class "Order" and give it Public accessibility.
Drag another Class item from the toolbox and drop it onto the design surface. In the New Class dialog box, name this class "OrderDetail," and give it Internal accessibility. The OrderDetail class will only be accessible from within the other classes of this project.
Because the two classes are related, the next step is to draw the association between the classes. Click on the Association item in the toolbar. The cursor changes to an association symbol. Click on the Order class first and then click on the OrderDetail class to define the association. The result is shown in Figure 2.
Figure 2: The Class Designer displays the classes of your application along with their relationships.
Figure 3: Designing Your Mobile Application: The Class Details window allows you to define the methods, properties, fields, and events for your class.
For a class to have any usefulness, it needs to have some functionality. You can add methods, properties, fields, or events to the classes. Add a CalculateTotal
method to the Order class that will calculate the total cost of the order. Right-click on the class and select the Add context menu or use the Class Details window that appears at the bottom of the Visual Studio editor when a class is active on the design surface as shown in Figure 3
Notice that the Class Details window displays an OrderDetail property in the Order class. The Class Designer automatically created this property when you defined the association between the Order and OrderDetail classes.
Now let's create a C# project in the same solution as the VB project. Follow the same process for creating a Class Designer and name it Customer. Drag the Class item from the toolbox and drop it onto the design surface to create the Customer class.
|Author's Note: When using C# with the May preview, Visual Studio generates an exception when attempting to use the Class Details window to add properties or methods or when you attempt to use the Association item.
You can use the resulting class diagrams as a way to communicate your application's design. You can print them, you can save them as an image and post them on your developer Web page, or you can include them in a presentation.