#766 – Adding an Interface to a Class Diagram

If you add a class to a class diagram and the class implements one or more interfaces, you’ll see the interfaces show up on the class with the standard interface designator (a short line with a circle at the end).



You can also add an element to the class diagram that represents the interface itself.  To do this, just right-click on an interface and select Show Interface.



Once you do this, the interface will be added to the diagram.  You can then expand the basic element to see the members of the interface.



#765 – Adding Base or Derived Classes to a Class Diagram

You can use the class diagram to follow the inheritance chain for any class currently in the diagram, moving up to the class’ base class or down to the classes that derive from the class.

For example, let’s say that you’ve dragged the ArgumentException class onto a class diagram, from the Class View.



You can add the base class of ArgumentException to the diagram by right-clicking on the class and selecting Show Base Class.



The base class will now be shown on the diagram.



You can also show all derived classes of a particular class by right-clicking on the class and selecting Show Derived Classes.



Using this technique, you can easily browse through the classes in the .NET Framework.


#764 – Expanding All Classes in a Class Diagram

By default, when you add types to a class diagram, they are displayed collapsed, showing only the name of the type and the category (e.g. class, interface).


You can expand individual types by clicking on the double downward facing arrow.  You’ll then see all the members of the type.

You can use the “+” key on the numeric keypad to expand the currently selected type.  You can also do the following, to quickly expand all types:

  • Select all typeson the diagram by pressing Ctrl+A
  • Expand all types by pressiong the “+” key on the numeric keypad
  • Right-click on the diagram and select Layout Diagram

Selecting Layout Diagram will reorganize all types so that they are layed out in a nice way.  The only problem is that types that are not connected to other types in the diagram will once again be collapsed.  You’ll need to expand and manually lay out these types.


#763 – Fixing a Class Diagram that Cannot Find Types

If you create a class diagram that contains types found in assemblies that your project references, and then you leave the class diagram open when you close Visual Studio, you may see some errors in the class diagram when you re-open the solution.  The types on the class diagram may all be drawn in red and report “Type cannot be found”.



You can fix this problem by just closing and re-opening the class diagram.  The diagram will re-discover the information for the types.



#762 – Creating a Class Diagram Containing Types in the .NET Framework

You typically create a class diagram in a project for the custom types that you’ve created in that project.  You can also include types on a class diagram that come from assemblies that your project references, including the .NET Framework itself.

To add elements to a class diagram representing types in the .NET Framework, do the following:

Add a new class diagram to the project.  (Right-click on project, select AddNew Item, then select Class Diagram).



Switch from Solution Explorer to Class View and expand Project References until you find a namespace or type that you want to look at.



Left-click and drag the selected namespace onto the class diagram.  All types from that namespace will show up on the class diagram (initially zoomed out).


You can zoom in and expand types to see more details.



#761 – Create or Modify Type Members Using the Class Details Window

You can use the Class Details window to easily create or modify a type’s members.

To open the Class Details window, start by opening a class diagram.  If the Class Details window does not appear, right click on one of the types in the class diagram and select Class Details.


Once the Class Details window opens, you’ll see a full list of the type’s members (e.g. methods, properties, fields and events).  The columns of the Class Details window shows some basic information about each member.  You can also expand methods to see parameters of the method.


To add new members, just start typing the name of the new member in the appropriate group.  For example, to add a new property, left-click on <add property> and start typing the name of the property.


You can also add members by clicking on the icon in the upper left corner of the window.


#760 – Adding New Class Members from a Class Diagram

In addition to adding new types from a class diagram within Visual Studio, you can also add members to existing types.

In the class diagram, you right-click the top area of the class and then click Add in the popup menu.  You’ll see a list of the different class members that you can add.


For example, we can add a RollOver method to the Dog class.  After selecting Method from the menu, a new method shows up in the list of methods for the class.


After you name the new method, different attributes of the method will show up in the Properties window on the right side of the Visual Studio workspace.  You can then make changes to the method by changing its properties.


You can of course go view or edit the new method in the code editor window.


#759 – Creating a New Type from a Class Diagram

You can create new types by directly entering code in the editor window.  You can also add a new type from a class diagram.

To start with, either open an existing class diagram or create a new one.

To create a new type, right-click on an empty area in the class diagram and select Add and then Class.  Note that you can also create other types (e.g. Enum, Interface, or Struct).



The New Class dialog will appear and you can enter the name of the new class and select an access modifier.  Note that the filename will be based on the class name by default.



After you click OK, the new type will show up in the class diagram.



You can now open your new type in the code editor window, to see the code that was generated for you.



#756 – Viewing a Class Diagram in Visual Studio 2012

You can create and view a class diagram from within Visual Studio that shows all of the types in your project and their relationships.  This is helpful in understanding how your project is structured.

To create a class diagram that allows viewing all types in your project, do the following.

In Solution Explorer, right-click the project and select View Class Diagram.



A diagram will appear that shows all of the types in your project.  In this example, the diagram also shows that Terrier is a subclass of Dog and that the Dog class implements the IBark and IFetch interfaces.

You can also click on the double down arrow icon to expand any of the types.  (You may have to move them around to make things look pretty again).



You can also navigate to the code for any member shown in the diagram, by right-clicking on the member and selecting View Code.