Refactoring Code Rename Members using Visual Studio

As I have been discussing about Refactor menu yesterday, Visual Studio comes up with really nice features on Refactoring code using the refactor menu options. In this post I will cover how to Rename your code using Refactor “Rename” menu option.

Let us suppose we have a member in our code called FetchMember, and we need to rename the member name, its all overloads and even everywhere it is used, all at a time, one thing you can do is to use Find option in visual studio, but if FetchMember is somewhat generalized and need to be changed only to this Type, and usage restricted to this type only, then manual task of rename would be lot of pressure.

Refactor Rename feature gives you easy means to solve in such scenarios. Let us look how to do it :

1. Select the member you want to Rename, and Press F2, or select Rename from Refactor Menu. Say we want to change name of a method FetchRecord to GetRecord of the following code.

protected virtual void FetchRecord(DataRow currentRow)
{
this.CurrentRow = currentRow;
this.IsExists = true;
}

2. Select the New Name in the dialog box that comes up, and select the options that are needed to be considered.

The “Preview reference changes” will indicate that the tool will show a new Dialog that will preview before changes are made. Always select this option to be in safe side.

“Search in comments” indicate that the name in comments need also be changed.

“Rename overloads” are if we need to change name of all overloads of the current Name.

“Search in strings” will search in string literals too.

3. Once we select all the options, we go to click On to do the actual Rename operation.

 

Now as we have selected “Preview reference changes” the Refactor in Visual Studio will go on and select all the method names on each Type associated with it to change names and will be presented in a new Dialog with a Tree.

 

You can see in the above dialog box, it searched one occurance of FetchRecord in DataObjectBase and will be converted to GetAllRecord.

You can individually choose the checkboxes from the Tree and make changes everywhere you need. The Nodes are responsive, I mean you can preview any node from the treeview upon clicking on the type and the actual final code will be shown on the pane below.

Finally when everything is Ok, select Apply to apply changes to every node that is checked.

 

I hope this comes handy.

Thank you for reading. Stay tune for more.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434