Dale Preston's Web Log
  
Tuesday, January 31, 2006
 

Encapsulating Fields with Properties that works

I was excited to see the refactoring tools in Visual Studio 2005. And I have really tried to use and like them but to tell you the truth, they hurt. Physically.

For instance, when I create a new class, I often start out by typing a whole list of private fields. This week, I must have created well over 200 private fields in classes and for the first half of the week, I also typed in half as many properties like:

public string Agency
{
get { return agency; }
set { agency = value; }
}

I had to type them out because the project is .Net 1.1 and Visual Studio .Net 2003. I only had to type half as many properties because I finally broke down and created a macro to do it for me. The second 100+ fields became properties in a matter of seconds with my EncapsulateFields macro.

Sometimes, when I am typing properties too much, I will paste the fields into Visual Studio 2005 and use the refactoring tools to create the properties code. What a frustrating experience! It makes my hand cramp! If I have a list of 50 fields in a class, to have to select each one, one at a time, go through the steps to encapsulate the field in a property, one at a time, is just painful!

Well, I created a simple solution. Now, there are more robust code generators that either are difficult to use, require lengthy difficult to write templates, or cost hundreds to thousands of dollars. And they do way more than my solution. But for pure simplicity, doing one thing and doing it very well, my Visual Studio macro is fantastic. And it works in both Visual Studio .Net 2003 and Visual Studio 2005.

So forget Visual Studio 2005's one at a time refactoring. Download my EncapsulateFields macro and save yourself a lot of frustration, hand cramps, and time!

The EncapsulateFields macro will turn
        private string agency;

Into
        public string Agency
{
get { return agency; }
set { agency = value; }
}

with just a double-click of the mouse. And it will do it 1 time or 100 times for the same double-click.

It saved me more time than it took to write on the first day I used it.
Comments:
I just have to say thank you. This is really a great macro.
Happy programming!
 
Hi Dale - is this still your current weblog - just checking my links.
 
sorry, but the encapsulatefields.zip file is missing. It is giving a 404 (file not found) error. thanks
 
Sorry about that. I guess that file got missed when my hosting service went belly up and I rebuilt the site recently.

The file is available for download now.
 
Post a Comment

<< Home

Powered by Blogger