Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#
Article

Adhoc Refactoring with Visual Studio's "Replace in Files"

Rate me:
Please Sign up or sign in to vote.
2.25/5 (3 votes)
13 Apr 2009CPOL2 min read 13.6K   4   3
One of my favorite refactoring tools is Visual Studio's "Replace in Files" dialog

Introduction

One of my favorite refactoring tools is Visual Studio's "Replace in Files" dialog. It pops up with the touch of the Ctrl+H key combo, sports all kinds of options, and operates against just the current file or set of files.

The most powerful option is the 'Use Regular Expressions' option. There are a few examples in MSDN to help get you started using regular expressions in Visual Studio. From there, you can quickly build up your own regex refactoring expressions library.

Using the Code

Example: Convert a set of automatic properties to the older style that will work in Visual Studio 2005.

This can be handy if you have code in a VS 2008 project that you would like to reuse in VS 2005. If you only have one or two automatic variables, let the compiler catch them and quickly make the changes by hand. But if they are scattered throughout many files, a few pre-canned regex expressions can decimate the amount of manual work required.

Steps

  1. Open an empty code file and paste in the sample code below:
    C#
    class Claim
    {
        // Works on multi line properties
        string PayerName 
        { 
            get;
            set;
        }
        // Or single line properties
        string PayerId { get;set;}
        string PayerIdQual { get;set;}
    }
  2. Hit Ctrl+H to bring up the Find in Files dialog. Make sure that Quick Replace is selected.
  3. Paste the regular expressions below into the Find What and Replace With text boxes:
    Find What: ^{:Wh*}{:i}:Wh*{:i}:Wh*\{:Wh*get:Wh*;:Wh*set:Wh*;:Wh*\}
    Replace With: \1private \2 m_\3;\n\1\public \2 \3 
    		{ get { return m_\3; } set { m_\3 = value; }}
  4. Click "Replace All".

Result

C#
class Claim
{
    private string m_PayerName;
    public string PayerName { get { return m_PayerName; } set { m_PayerName = value; }}
    private string m_PayerId;
    public string PayerId { get { return m_PayerId; } set { m_PayerId = value; }}
    private string m_PayerIdQual;
    public string PayerIdQual { get { return m_PayerIdQual; } 
				set { m_PayerIdQual = value; }}
} 

Points of Interest

Generic Properties

You may have asked yourself, "What happens if I have an automatic property with a private or protected setter or the property is a generic collection?" As the scenarios become more complex, decisions and tradeoffs begin to enter the picture.

The regex can be coded to handle multiple cases, but it can quickly become hard to understand and maintain. However going with 'lots of little' regexes can lead to different maintenance issues and it is a hassle to cut and paste all these regexes into the Find and Replace text boxes (however it can still be a huge productivity increase when you have to refactor hundreds of test cases in a hurry).

The next article will look at the strategies and alternative designs available with Visual Studio.

In either case, it is not too difficult to modify regexes once you understand how they work. You will find that with a little practice you can quickly create very powerful expressions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Moim Hossain13-Apr-09 4:52
Moim Hossain13-Apr-09 4:52 
GeneralRe: My vote of 1 Pin
Mike Lang13-Apr-09 6:19
Mike Lang13-Apr-09 6:19 
GeneralRe: My vote of 1 Pin
conanc13-Apr-09 18:53
conanc13-Apr-09 18:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.