#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.



#758 – Cleaning Up using Directives in a File

You use using directives at the top of a file to bring various namespaces into scope, so that you don’t need to fully qualify named members within those namespaces.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

Extra using directives don’t hurt anything–the list of directives is just a list of possible namespaces to consult when resolving a name.  Your code may not actually reference members in all of the listed namespaces.

For cosmetic purposes, however, there is an easy command in Visual Studio that you can use to clean up using directives for namespaces that you don’t use.

To remove unused namespaces, you can right-click in the source code editor and select Organize Usings and then Remove Unused Usings.  After you do this, you’ll be left only with namespaces that contain members that your code contains.




#757 – Books on Object-Oriented Programming

Since C# is an object-oriented language, it can be helpful to study object-oriented design and programming techniques.  Here are some good books that focus on object-oriented design or programming.

  • Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd edition). Craig Larman. 2004.
  • Beginning C# Object-Oriented Programming. Dan Clark. 2011.
  • Design Patterns: Elements of Reusable Object-Oriented Software. Gamma, Helm, Johnson, Vlissides. 1994.
  • Designing Object-Oriented Software. Wirfs-Brock, Wilkerson, Wiener. 1990
  • Growing Object-Oriented Software, Guided by Tests. Freeman, Pryce. 2009.
  • Head First Object-Oriented Analysis and Design. McLaughlin, Pollice, West. 2006.
  • An Introduction to Object-Oriented Programming (3rd edition). Timothy Budd. 2001.
  • Object-Oriented Analysis and Design with Applications (3rd edition). Booch, Maksimchuk, et al. 2007.
  • Object-Oriented Design and Patterns. Cay S. Horstmann. 2005.
  • Object-Oriented Software Construction (2nd edition). Bertrand Meyer. 2000.
  • The Object-Oriented Thought Process (3rd edition). Matt Weisfeld. 2008.
  • OOP Demystified. Keogh, Giannini. 2004.
  • Principles of Object-Oriented Analysis and Design. Martin, Odell. 1993.

#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.


#755 – Viewing Class Members Directly in the Solution Explorer

In Visual Studio 2012, you most often use Solution Explorer to see the files within your solutions and projects.  Double-clicking on a file opens it in the code editor window.



However, you can also see information about the types in your code, using the Solution Explorer.  By using the little arrows to the left of each file, you can open up the file to see all of the types defined in the file.  You can then drill down into each type to see all of its members.  Double-clicking on a specific member will open the code editor window and navigate to the declaration of that member.


#754 – Downcast to a Reference to a Derived Class

If you have a variable that is a reference to a parent class, but it actually refers to an instance of a derived class, you can use a downcast operation to assign the reference to a variable whose type matches the derived class.

For example, assume that you have a Dog class and a Terrier class, which inherits from Dog.  You might have a reference to a Dog that actually refers to an instance of a Terrier.  To get at methods that are unique to Terrier, you’d downcast the Dog reference to a Terrier reference.

This type of conversion is known as a reference conversion.

            // Dog reference that points to a Terrier
            Dog d = new Terrier("Jack", Terrier.GrowlFactor.Severe);

            // Can't invoke Terrier methods via Dog reference
            //d.DoTerrierDance();   // ERROR

            // Downcast to Terrier.  Need explicit conversion
            Terrier t = (Terrier)d;

            // Can now treat like Terrier

#753 – Implicitly Upcast to a Base Class Reference

You can convert a reference to a child class to a reference to its base class using an upcast.  (“Up” indicates that you’re moving up the inheritance tree).  You can do this upcast implicitly, meaning that you don’t need a cast operator that indicates the target type.

For example, assume that Terrier is a subclass of Dog.  If we have a reference to a Terrier object, we can assign that reference to a variable that is a reference to a Dog.

            Terrier t = new Terrier("Jack", Terrier.GrowlFactor.Severe);

            // Implicit upcast - aDog now refers to Jack the Terrier
            Dog aDog = t;

After the upcast, we have a reference of type Dog that refers to an instance of a Terrier.

#752 – C# Does Not Support Multiple Inheritance

Some other object-oriented languages, like C++ and Python, support multiple inheritance.  Multiple inheritance allows a class to inherit data and behavior from more than one parent class.

C#, on the other hand, does not support multiple inheritance.  Every class inherits from exactly one class.  Multiple inheritance can lead to certain problems in object-oriented languages.  (E.g. the “diamond problem“).  C# avoids these problems by supporting only single inheritance.

While a class in C# can only inherit from a single base class, it can implement a number of different interfaces.