This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

VS 2005+

Renaming Identifiers in Visual Studio

Whilst refactoring code, it is often necessary to change the name of an identifier for a type, method, property or variable. Any name changes must then be reflected throughout the solution. This process is made simple with Visual Studio's Rename command.

Rename Command

Visual Studio 2005 and newer releases include some refactoring tools. These tools make it easier to restructure code as requirements change, or as a part of a development methodology, such as Test-driven Development. One of the tools available directly from the integrated development environment is the Rename command. It permits identifier names to be changed, including the names of variables, fields, methods, parameters, properties, events, classes, structures and namespaces. Any references to those names throughout the entire solution are updated accordingly so that the program still compiles correctly.

The Rename command considers more than just the selected identifier. If a method name is being modified and that method include overloaded versions, an option is available to change the names of the overloads too. When renaming a class member that is inherited from a base class or interface, the originating member's name is also changed. As the process is iterative, all other members that are derived from the base class or interface will also be renamed.

A less used but powerful option when using the Rename command is to extend the search further. This allows the contents of strings and comments to be considered and modified during the renaming process.

Source Control Consideration

When using the rename command with a solution that is held in a source control system, ensure that you have the latest version of all files before refactoring. If you do not, it is possible that references to the changed identifier will exist in files that are not up to date. These names may then be missed.

Renaming Identifiers

Renaming Identifiers in the Code Editor

The usual place to change identifier names is within the code editor. Firstly the identifier to modify must be specified by clicking it. You can then select the Rename command from the Refactor menu. You can also right-click an identifier and select the command from the context-sensitive menu that appears.

Visual Studio Rename Command

Once the command has been selected, the Rename dialog box is displayed, pictured below.

Visual Studio Rename Dialog Box

The Rename dialog box allows the operation to be controlled using a set of options. The "New name" textbox initially shows the current name of the selected identifier. This allows you to ensure that you have selected correctly. The new name should be entered here. The other options appear as a set of up to four checkboxes.

  • Preview reference changes. If this option is checked, another dialog box appears when OK is clicked. This shows all of the instances of the identifier that have been found by Visual Studio. A checkbox is placed alongside every change that will be made so that you can decide to exclude some modifications.
  • Search in comments. If this option is selected, any references to the old name that are found within comments are also updated. This is useful if comments refer to other methods or if an area of code has been commented out temporarily.
  • Search in strings. This option is rarely used. It modifies the contents of strings that include the name of the identifier being changed.
  • Rename overloads. This option, when selected, causes the names of overloaded versions of a method to be modified to match the updated identifier.

Renaming Identifiers in the Solution Explorer

The Solution Explorer shows the names of all of the files that exist within the solution's projects. If the name of a file is changed, and the filename matches the name of the underlying class, the option is given to perform rename refactoring for the class.

Renaming Identifiers in the Class View, Class Diagram or Object Browser

The Rename command is available when using the Class View, a class diagram or in the Object Browser. In these cases, the classes and their members can be changed. This includes private members but not local variables.

Renaming Identifiers in the Windows Forms Designer

The Windows Forms Designer permits some use of rename refactoring. Within the properties grid, the name of a control or component may be modified to begin the refactoring process.

25 September 2008