1. Technology

Classes - The Class Diagram Tool in Visual Studio

Use Visual Studio to the Max!

By

Hands
dm909/Moment/Getty Images
Updated June 18, 2014

About Visual Basic features a number of articles explaining how both classes and inheritance work. You can start with:

-> An Introduction to Programming a VB.NET Control With Inheritance

Then move on to:

-> Programming a Custom VB.NET Control

Those articles are focused on the practical understanding of controls and inheritance and show you how to build your own custom controls using classes. The first builds a custom CheckBox control that displays the CheckBox in different colors and different symbols instead of just a box with a check in it. The key thing to keep in mind is that the new CheckBox (imaginatively named "newCheckBox" in the article) is a class that "inherits" the VB.NET supplied code for a standard CheckBox and then modifies that code to add the new features.

This article focuses on the use of the Visual Studio Class Designer which can chart out your project in a picture. This tool helps you organize, understand, and code your classes using Visual Studio. Class diagrams ...

... help you understand the structure and content of your code. "A picture is worth a thousand words!"
... illustrate the syntax of the code. The meaning of inheritance terms like Overloads and Overrides can be seen.
... help you code the program. The tool not only shows you the code in your program, it can also help you add or change the code.

One of the most useful things you can do with class diagrams is to chart out a complex project that you're unfamiliar with to figure out how things work. As an example, consider this code for Automated Recurring Billing (ARB) that I downloaded from the web. (There's too much code in this project to see the whole thing in one window ... the idea is that you can get a visual picture of the whole system to get started.)

--------
Click Here to display the illustration
--------

If you work for an organization that supplies you with Visual Studio Ultimate (or if you simply have money rolling out of your ears and you can buy your own copy), the Ultimate Class Diagram tool provides UML (Unified Modeling Language) diagramming. Using VS Ultimate, you can not only diagram your project in UML, you can generate code directly from the UML.

This article is based on the much more affordable Visual Studio Professional. You can add code from the diagram in this version, but "code generation" isn't possible.

Side Note - Skip if you're in a hurry

Microsoft has had a hot-and-cold relationship with UML. I wrote about this in an article written a few years ago:

In the summer of 2004, Microsoft finally let the world know what they were going to do about the fact that IBM now owned much of the UML software market. (IBM acquired Rational Software which "owned" most of the UML market.) They announced a new product that will be included in VS.NET 2005 called Team System for Visual Studio. Marie Huwe, general manager for Microsoft's Developer and Platform Evangelism Division was quoted as saying, "We're designing for ease of use, and we didn't think UML was the way to go." Funny, they used to think that UML was the way to go.

Starting with Visual Studio 2010, Microsoft has now seen the error of their ways and they must think that UML is "the way to go" again because UML is back in their product line.

End of Side Note

To get started, I created a simple Windows Forms application and just one class:


Public Class Form1
    Private Sub Form1_Load(
        sender As System.Object, e As System.EventArgs
        ) Handles MyBase.Load
        ' No Code!
    End Sub
End Class
Public Class Class1
' No Code! Just the Class Block.
End Class

A class diagram can be created by clicking on the Class Diagram icon (when the project is selected) in Solution Explorer. The diagram boxes are expanded (shown here) by clicking the double chevron icon in the upper left corner.

--------
Click Here to display the illustration
--------

This is the simplest example possible, but it still shows quite a bit of information. For example, the diagram shows that the base class of Form1 is the Form class. In fact, by right-clicking the diagram box and selecting Show Base Class, you can create a diagram of the objects that Form1 inherits from all the way back to the Object object, complete with all of their interfaces. This is an example of how class diagrams can help you understand .NET.

--------
Click Here to display the illustration
--------

Just for fun, I expanded the Control class diagram box and then zoomed the window out as much as Visual Studio allows just to show how much information is available in the Control diagram. There's so much that you can't see the detail here so if you want to see for yourself, you'll have to recreate the illustration.

--------
Click Here to display the illustration
--------

The other parts of a Solution can be added to the diagram as well, including those that are normally hidden in Solution Explorer.

--------
Click Here to display the illustration
--------

A new class, named Class2 and declared Friend (accessible only from within the assembly that contains the declaration), was added to the diagram and Sub's with all possible modifiers were coded in that class to demonstrate even more information available.

--------
Click Here to display the illustration
--------

Virtually all of Microsoft's class diagram documentation applies to the more expensive Visual Studio Ultimate, but the tools in Professional are great too. Give it a try!

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. The Class Diagram Tool in Visual Studio

©2014 About.com. All rights reserved.