
Generate Code from UML #81
Chapter 10, Work with Visual Studio Tools
|
355
HACK
Figure 10-34 shows a class diagram that documents a Vehicle class hierar-
chy. This rather simple example shows an abstract base class named Vehicle
that contains a few properties (Make, Model, Year) and methods (Acceler-
ate, Decelerate, Drive, Start, Stop). The class diagram then shows other
implementation classes and their relationships to one another.
Generate the Code
Now that you’ve created a class diagram in Visio, follow these steps to auto-
generate the code in Visual Studio:
1. On the File menu, click UML
➝ Code ➝ Generate. The Generate dialog
box will appear.
2. For Target Language, select C# (or C++ or Visual Basic).
3. Type a Project Name and select a Location to put it in.
4. Check the “Add classes to Visual Studio Project” checkbox.
5. Select the template to use. The template list contains several Visual Stu-
dio Project types to choose from.
6. Type a Solution Name and check the Create Directory for Solution
checkbox.
7. Select all classes for which you want to generate code.
8. Click OK. A sample Generate dialog is shown in Figure 10-35.
Figure 10-34. Class diagram of Vehicles class hierarchy
Vehicle
+ Make : string
+ Model : string
+ Year : string
+ Accelerate()
+ Decelerate()
+ Drive()
+ Start()
+ Stop()
Car
Van
Sportscar
Minivan
ExcursionVan
+CargoNet : bool
+DualSlidingDoors : bool
<<inherits>>
<<inherits>>
<<inherits>> ...