Refactoring Classes and Types (Class Designer)

When you refactor code, you make it easier to understand, maintain, and more efficient by changing its internal structure and how its objects are designed, not its external behavior. Use Class Designer and the Class Details window to reduce the work that you have to do and the chance of introducing bugs when you refactor Visual C# .NET, Visual Basic .NET, or C++ code in your Visual Studio project.

Note

The files of a project might be read-only because the project is under source-code control and is not checked out; it is a referenced project; or its files are marked as read-only on disk. When you work in a project in one of these states, you will be presented with various ways to save your work depending on the project’s state. This applies to refactoring code and also to code that you change in another way, such as directly editing it. For more information, see Display of Read-Only Information.

Common Tasks

Task

Supporting Content

Refactoring classes:You can use refactoring operations to split a class into partial classes or to implement an abstract base class.

Working with interfaces:In Class Designer, you can implement an interface on the class diagram by connecting it to a class that provides code for the interface methods. If you are using Visual C#, you can also extract one or more public members from a type into a new interface.

Refactoring types, type members, and parameters:By using Class Designer, you can easily make changes such as renaming a type or type members. You can override type members or move them from one type to another. You can create easily create nullable types. And, if you are using Visual C#, you can reorder method parameters.

Implementing Abstract Base Classes

You can use Class Designer to implement an abstract class.

For this procedure, the following is assumed:

  • Your project contains an abstract class.

  • The abstract class contains abstract members.

  • The abstract class is the base class in an inheritance relationship with another class. (The derived class does not have to be an abstract class.)

Implement an Abstract Class

  • Right-click the derived class, click IntelliSense then click Implement Abstract Class. All abstract members from the base class are implemented in the derived class.

For additional information, see How to: Create Inheritance Between Types (Class Designer) and How to: Create Types by using Class Designer.

Extracting Members to an Interface

You can extract one or more public members from a type into a new interface.

To extract members to a new interface

  1. In Class Designer, right-click the type that contains the member or members you want to extract, point to Refactor, and then click Extract Interface.

    The Extract Interface dialog box displays default values for the name of the interface and the name of the code file in which it will be declared. Either accept the default values or change them.

  2. In the Select public members to form interface pane, select or clear the check box next to the members you want to extract into the new interface, and then click OK.

    A new interface is created, and the file that houses it is added to the project.

Overriding Type Members

In Class Designer, you could allow members such as methods and properties in a child class to override members inherited from a base class. Overriding a member is possible only if the following circumstance applies:

  • The base method being overridden must be virtual, abstract, or override. (It cannot be non-virtual or static.)

To override a member

  1. On the class diagram, right-click a class shape, and click IntelliSense then OverrideMembers.

    A dialog box displaying overridable members appears.

  2. From the list, specify a member.

    A member with the same name, access modifier, and return value, appears in the class, and its Inheritance Modifier property is set to Overrides in Visual Basic, or override in C#.

Renaming Types and Type Members

In Class Designer, you can rename a type or a member of a type on the class diagram or in the Properties window. In the Class Details window, you can change the name of a member but not a type. Renaming a type or type member propagates to all windows and code locations where the old name appeared.

To rename a name in the Class Designer

  1. On the class diagram, select the type or member.

    The name of the member becomes editable.

  2. Type the new name for the type or type member

To rename a name in the Class Details Window

  1. To display the Class Details window, right-click the type or type member and then click Class Details.

    The Class Details window appears.

  2. In the Name column, change the name of the type member

  3. To move focus away from the cell, press the ENTER key or click away from the cell.

    Note

    In the Class Details window, you can change the name of a member but not a type.

To rename a name in the Properties window

  1. On the class diagram or the Class Details window, right-click the type or member and then click Properties.

    The Properties window appears and displays properties for the type or type member.

  2. In the Name property, change the name of the type or type member.

    The new name propagates to all windows and code locations in the current project where the old name appeared.

Reordering Parameters

In the Class Designer, you can change the order of method parameters in types.

Note

Optional parameters must be the last parameters in the parameter list. For information about how to create optional parameters using Class Designer, see Adding Parameters to Methods.

To change the order of parameters

  1. On the class diagram, expand the type shape and then the Methods compartment.

  2. In the Methods compartment, right-click the method with the parameters you want to reorder, point to Refactor, and click Reorder Parameters.

    The Reorder Parameters dialog box appears.

  3. In the Reorder Parameters dialog box, move the parameters into the order you want, and then click OK.

    The parameter order changes in code and in the shape on the class diagram.

Moving Type Members from One Type to Another

Using Class Designer, you can move a type member from one type to another type, if both are visible in the current class diagram.

To move a type member from one type to another

  1. In a type that is visible on the design surface, right-click the member you want to move to another type, and then click Cut.

  2. Right-click the destination type and then click Paste.

    The property is removed from the source type and appears in the destination type.

Title

Description

Viewing Types and Relationships (Class Designer)

Designing Classes and Types (Class Designer)