Class Hierarchies


Visual Studio 2005 offers several visual tools that depict class hierarchies. These tools also depict individual types, such as classes, structures, and interfaces. This includes showing relationships between types, including inheritance. The Class View and Object Browser have been updated with new features. The class diagram is new and adds a capable object-modeling tool to Visual Studio 2005.

Class View Window

The Class View window provides a visual representation of the types included in projects of the current solution. Expand the project and then expand class nodes to display the class hierarchy. Each item in the class hierarchy has a shortcut menu with helpful commands. For example, the shortcut menu can display classes in a class diagram. The Class View window can now show classes in referenced applications. Open the Project References folder to find references and view their classes. Figure 4-10 shows the Class View window, including the Project Reference folder. If the Class View window is closed, open it from the View menu.

image from book
Figure 4-10: Class View window

The Class View window has two panes: The top pane contains the hierarchy, whereas the bottom pane displays the details of whatever item is selected in the class hierarchy. Across the top of the class view window are four icons: New Folder, Forward, Back, and Class View Settings. Beneath the icons is the search combo box, which is used to search the hierarchy for a symbol. This is especially helpful for extensive class hierarchies. Type the search text and press Enter to initiate a search. All symbols—such as classes, methods, fields, and properties, which contain some part of the search text—are returned in the Class View window.

Object Browser

The Object Browser is an alternative to the Class View window. The Object Browser displays the class hierarchy of the current project, referenced applications, and system libraries. If the Object Browser is not visible, open it from the View menu. Figure 4-11 shows the Object Browser window.

image from book
Figure 4-11: Object Browser window

The Object Browser window contains three panes. The left pane contains the class hierarchy. The two right panes list the members and a description of the selected type, respectively. The Object Browser toolbar has the Browser Scope, Edit Custom Component Set, Back, Forward, Add Reference, and Object Browser Settings buttons. The Browser Scope button filters the class hierarchy. You can display classes from the current solution, custom component set, .NET Framework, or all classes. Browse a class in a foreign assembly using the Edit Custom Component Set button. The Add Reference button adds a reference to the project for the application of the selected type. Above the class hierarchy is a combo box for searching. Use the search box to find specific types in the class hierarchy.

Class Diagram

Visual Studio 2005 introduces the class diagram. Class diagrams are created by the Class Designer, which is an internal component of Visual Studio 2005. Developers can model object-oriented applications using class diagrams. This tool will assuredly continue to evolve in the future and is destined to be a developer favorite. For me, it has already become one of the most frequently used tools in Visual Studio. Class diagrams present a visual representation of a type and type hierarchy. It's important to note that class diagrams are not static. Developers can add new types, create new relationships, insert members, delete members, and much more. The class diagram is synchronized with the source code of the application. Changes to types modeled in the class diagram are immediately reflected in code. Conversely, changes in the source code immediately appear in a relevant class diagram. Therefore, the application and diagram consistently remain in sync. Updated design documents are one of the seminal benefits of the class diagram. Where do design documents vanish to after the project implementation has begun? This is a common problem that plagues developers of object-oriented applications. For many applications, original design documents are not available or have not been updated as the software evolved. Often, design documents quickly become stale and sometimes vanish shortly after the implementation phase. Class diagrams help update these important documents, including changes to the original design. Conversely, class diagrams can be saved as images preserving a snapshot of the design. With the class design, the design and implementation phase is truly iterative, which translates into better-developed applications. It also makes maintaining applications exponentially easier.

Class diagrams provide a high-level perspective of an application, which is beneficial throughout the life cycle of the application. This is particularly useful with complex systems that entail hundreds of classes and dozens of relationships. Reviewing code in this circumstance is a tedious, time-consuming process. It also does not transfer the clarity of understanding that only a synoptic view of an application affords. Class diagrams present a general overview, in which introspection is available as needed. This information is particularly invaluable to programmers maintaining an application. The class diagram represents a new starting point for anyone who has to maintain a product with Visual Studio.

The entire class hierarchy can be presented in the class diagram. However, developers control how much information is presented in the class diagram. You can view one class, a dozen types, or everything in the class diagram. It depends on developer discretion. In addition, multiple class diagrams can be added to group-related types or simply to reduce the amount of information presented in any particular diagram. In the diagram, you can view all types: classes, structures, and interfaces. Relationships between types, such as inheritance and association, are also available.

In Visual Studio 2005, there are several ways to create a class diagram. One way is to select the Add New Item command on the Project menu. From the Add New Item dialog box, choose Class Diagram. In the edit box, name the class diagram. (Class diagram files are automatically given the .cd file extension.) You can also open a new class diagram from the Solution Explorer or Class View window. In Solution Explorer, open a shortcut menu for the project name or source file and then choose View Class Diagram. This creates a new class diagram that contains all the classes found in the project or source file. In Class View, open a shortcut menu for the project name or class and choose View Class Diagram.

Class diagrams have a surface. There are several ways to add existing or new types to this surface, as listed here. Types added to the class diagram surface are represented by shapes.

  • Drag a type from the Class View window or the Object Browser to the class diagram surface.

  • Drag a file from Solution Explorer to the class diagram surface.

  • Add a new type from the Class Designer toolbox.

Figure 4-12 shows a class diagram with a single class on the surface. The single class is contained in a shape. Shapes have a header and a details pane. Click once on the class label in the shape header to change the name of the class. Double-click the class label to view the related source code. There is also an expand button or collapse button. The expand button is the double-down arrow and shows the details of the type. The collapse button is the double-up arrow and hides the details of the type. The expand and collapse buttons toggle, depending on whether the details are expanded or collapsed.

image from book
Figure 4-12: Class diagram that contains a single class

Types can be removed from a class diagram. Select the shape for the type and open the shortcut menu from the class header. Choose Remove From Diagram. The type is removed from the diagram, but remains in the program. The Delete Code option is not the same. This option not only removes the class from the class diagram but also deletes it from the project. An easier method of removing a class from the diagram is simply to select the type and then press Delete.

The class detail shows the members of the class. Members are grouped by type, where fields, methods, properties, and other member types are grouped. Each group can be collapsed or expanded. From the shortcut menu, members can be refactored, deleted, copied, pasted, and more. Members can also be viewed and maintained in the Class Details window, as shown in Figure 4-13. If the Class Details window is not visible, display it from the shortcut menu of any shape in the class diagram.

image from book
Figure 4-13: Class Details window

In the Class Details window, you can view, change, or add new members to the class. You can change the name, type, or accessibility of a type. The Hide option hides or displays a member in the class diagram. The shortcut menu of the class header has the Show All Members command, which shows all members, including any hidden members.

The Class Details toolbox has four buttons. The New Member button adds a Method, Property, Field, Event, Constructor, Destructor or constant to a class. The Navigate To Methods button selects that method category in the Class Details window. This is helpful for large classes with dozens of members. The final three buttons navigate to properties, fields, and events, respectively.

Class Diagram Toolbox

Class diagrams have a specific toolbox, as shown in Figure 4-14. The top buttons of the toolbox add new types to the application and the class diagram. You can add a new class, enumeration, interface, abstract class, structure, and delegate. Double-clicking the associated button adds the new type to the application, which is then displayed in the class diagram. Alternatively, you can drag the related button from the toolbox onto the class diagram surface to add the new type. The New Type dialog box is displayed when a new type is added. (See Figure 4-15.) The type name, accessibility, and file name can be entered into the dialog box.

image from book
Figure 4-14: Class Diagram toolbox

image from book Figure 4-15: Add New Interface Dialog box

After the buttons for adding new types, the inheritance and association buttons appear next on the toolbox. The inheritance button creates an inheritance line, which links a base class and a derived class. The association button creates an association line that defines the relationship between an embedded class and the owning class. The comment button is the last item on the toolbox and adds comments to the class diagram.

Inheritance

Inheritance is visualized in the class diagram. Inheritance lines depict the base-to-derived-type relationship. Implicit inheritance of System.Object and System.ValueType are not shown in the class diagram. Figure 4-16 shows class inheritance in the class diagram. In the figure, XClass1 inherits the ZClass class. The inheritance line is the arrow that starts at the base class and ends at the derived class. Delete the inheritance line to remove the inheritance relationship. Alternatively, you can remove the inheritance line from the shortcut menu. Open a shortcut menu on the inheritance line and then select Delete Code.

image from book
Figure 4-16: Class inheritance in the class diagram

You can define new base and derived classes in the class diagram. Select the inheritance line in the toolbox. Drag the inheritance line from the base class to the derived class. This assumes that both the base and derived classes are already on the class diagram. If the base class is not on the class diagram, drag the class from the Class View window onto the derived class shape in the class diagram. This both creates the inheritance relationship and adds the derived class to the class diagram.

Interface inheritance and class inheritance are similarly shown in the class diagram. Add interface inheritance to a type using the inheritance line. To fully implement the interface, the class diagram adds stubs for each member of the interface in the derived type. In the Code Editor, developers can replace the stubs with the appropriate implementation. Interface inheritance is not depicted as an inheritance line. Instead, interface inheritance is displayed as a lollipop atop the derived type. This is displayed in Figure 4-17, in which the ZClass class inherits the IA interface. You can change how the interface is implemented using the shortcut menu of the interface label. This includes whether to implement the interface implicitly or explicitly.

image from book Figure 4-17: Interface inheritance in the class diagram

Relationship lines, which are the inheritance and association lines, can be rerouted. Simply click the line and drag the mouse to reroute. Lines can be rerouted multiple times. Figure 4-18 shows a rerouted inheritance line. Dragging the endpoints of the line repositions the relationship line on the class shape. The mouse cursor looks like a cross when positioned over the endpoint of a relationship line. The shortcut menu of a relationship line can hide, reroute, delete, or display the properties of the relationship.

image from book
Figure 4-18: Rerouted inheritance line

Class diagrams can find the base or derived class of a type. Open the shortcut menu on the class shape header. The Show Base Class command finds the base class of the type in the class diagram. If not already present, the base class is added to the class diagram. The Show Derived Classes command selects the derived type.

Association

Association lines define a "has-a" relationship, in which a class owns another class. The class is embedded as a property. Figure 4-19 shows an association relationship in the class diagram. In the figure, the XClass class owns the YClass class, which is embedded as a property. The association line looks slightly different from the inheritance line.

image from book Figure 4-19: Association relationship

Select the association line in the class diagram toolbox to create a new relationship. Both classes and interfaces can be embedded. Drag the association line from the owning type to the embedded type. A property is created in the owning class for the embedded class. You can then switch to the Code Editor and implement the property, which should return an instance of the embedded type. When the association line is visible, the embedded member is not shown in the class details pane of the shape. If desired, you can show the embedded member and hide the association line. Open the shortcut menu of the association line and select the Show As Property command. To reverse that decision, open the shortcut menu and select Show As Association.

Class Diagram Walkthrough

This walkthrough demonstrates the class diagram—particularly creating new types and relationships.

  1. Create a new class library. In Visual Studio 2005, from the File menu, choose the New submenu to create a new project. You can also accomplish this by clicking New Project on the Standard toolbar. The Class Library template is selected from a C# project. The project is named Personnel.

  2. Add a class diagram to the project. Open the Project menu and select the Add New Item menu item. From the Add New Item dialog box, add a class diagram. The class diagram is named Employee.

  3. Add a new interface using the class diagram toolbox. Name the interface IEmployee. Other defaults are accepted.

  4. Add a new abstract class using the class diagram toolbox. Name the class IEmployee.

  5. Add another class using the class diagram toolbox. Name this class HourlyEmployee.

  6. Add a new struct using the class diagram toolbox. Name the struct Name. Structs are depicted in the class diagram as rectangles with square corners.

From the Class Details window, add three members to the IEmployee interface. (See Figure 4-20.) The EmployeeInfo method returns a string. The Age property is an integer and gets and sets the age of an employee. Finally, the Fullname property is of the Name type and gets the Employee name.

image from book
Figure 4-20: IEmployee interface details

The Employee class should inherit and implement the IEmployee interface. Select the inheritance line in the class diagram toolbox. Drag the inheritance line from the Employee class to the IEmployee interface. The interface members now appear in the class details of the Employee class. In addition, Employee is given the stubbed implementations of the interface members:

 public abstract class Employee : IEmployee {     #region IEmployee Members     public int Age     {         get         {             throw new Exception(                 "The method or operation is not implemented.");         }         set         {             throw new Exception(                 "The method or operation is not implemented.");         }     }     // Partial listing 

Derived types of the Employee class must have a calculate pay operation. Add an abstract CalculatePay method to the Employee class. The method returns a decimal. In the header of the Employee class, open a shortcut menu. Select the Add submenu and the Method menu command. Name the new method CalculatePay. In the Class Details window, set decimal as the return type. The method should be abstract. Open the shortcut menu for the CalculatePay method and change the Inheritance Modifier to abstract. A message box is displayed asking you to confirm this decision. Click Yes.

The HourlyEmployee class should inherit the Employee class. This relationship is created with the inheritance line. Drag the inheritance line from the HourlyEmployee class to the Employee class.

Finally, add a Pay method to the HourlyEmployee class. The Pay method has a single parameter, which is the hours worked. In the Class Details window, expand the row for the Pay method to expose the Add Parameter item. Select the Add Parameter row and enter Hours as the parameter name. Change the type to decimal. To complete the class, add a HourlyRate property to the class. It is a decimal type.

The Name struct has two string properties: FirstName and LastName. Both properties can be added in the Class Details window.

The final class diagram for the Personnel application is shown in Figure 4-21.

image from book
Figure 4-21: Class diagram of the Personnel library

This is code created per the class diagram. The functions are stubbed. Implementing the stubbed methods is the only remaining step:

 public interface IEmployee {     int Age     {         get;         set;     }     Name Fullname     {         get;         set;     }     string EmployeeInfo(); } public struct Name {     public string FirstName     {         get         {             throw new System.NotImplementedException();         }         set         {         }     }     public string LastName     {         get         {             throw new System.NotImplementedException();         }         set         {         }     } } public abstract class Employee : IEmployee {     #region IEmployee Members     public int Age     {         get         {             throw new Exception(                 "The method or operation is not implemented.");         }         set         {             throw new Exception(                 "The method or operation is not implemented.");         }     }     public Name Fullname     {         get         {             throw new Exception(                 "The method or operation is not implemented.");         }         set         {             throw new Exception(                 "The method or operation is not implemented.");         }     }     public string EmployeeInfo()     {         throw new Exception(             "The method or operation is not implemented.");     }     #endregion     public abstract decimal CalculatePay(); } public class HourlyEmployee : Employee {     public decimal HourlyRate     {         get         {             throw new System.NotImplementedException();         }         set         {         }     }     public override decimal CalculatePay()     {         throw new Exception(             "The method or operation is not implemented.");     }     public void Pay(decimal Hours)     {         throw new System.NotImplementedException();     } } 

Error List Window

The Error List window is new to Visual Studio 2005. It displays edit and compile errors, warnings, and general messages. Unique icons are assigned to each type of message. For example, error messages are decorated with a red circle that contains an x. If there are compile errors, compiling a program automatically displays the Error List window. (See Figure 4-22.) You can also display the Error List window from the View menu.

image from book
Figure 4-22: Error List window

The Error List window displays different categories of messages. The Error, Warning, and General buttons hide or show a particular category of messages. The Error List buttons also indicate the number of messages in each category. For each error, the error number, description, and location are shown. Double-clicking an error message in the error list will jump to the related source code. In addition, you can use the column headers to sort the error list. Column headers can be dragged to change column order.




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net