How to build a class diagram for your projects in Visual Studio 2008/2010 automatically

Standard

One of the great features of Visual Studio is it’s ability to easily create diagrams showing the relationships and inner workings of your classes for applications you are building.  Visual Studio allows you to create class diagram files (.cd) in all kinds of different projects such as visual c++ applications.  This diagram file has a design view interface that allows you to drag and drop classes from your solution explorer or class view.  The display will show the class with all its properties and methods and which access level they have.  It will also show relationships between classes and inheritance relationships.

Soundcard Class

Here is an example of a class showing in the design view

From this you can see a number of useful things.  You can see that the class name is SoundCard, it inherits from the iSoundCard interface, and it has a number of fields (properties) and methods (functions).

The permissions of the fields and methods are represented through the images to the left of each field or method name.  Here is a summary of the main ones

  • Private const field
  • Private field
  • Public method
  • Private method
  • Protected method

There are a number of other features the class diagram has such as showing inheritance, macros, structs, and allowing you to save the diagram as an image.

To create a new class diagram just choose Add > New Item on a source folder you want to place it in.

Then select Class Diagram

Then you will be able to create a full class diagram from your classes in your project.

Now I’m sure the next question your asking is what if I update my class or remove it or add a new class ?

Well when you make changes the diagram automatically updates itself for you.  All you have to do is print it again or re-save the image if you are using it outside Visual Studio.

Here is an example of a fully formed class diagram based on the framework we work with in our Game programming course at Seneca.  This framework was written by Chris Szalwinski and is used by GAM666/DPS901 students.