April 2005 - Posts

Poor Man VS 2003 Tips & Tricks

Using Regular Expression to automate Properties, and Parameter List Creation

Often when creating classes for my application I find myself repeatingly doing the tedious task of converting the class' private members into properties and also as parameter list for the class constructor (for initialization).   Of course, you can make things easier by installing add-on / plug-in into your VS.NET such as Resharper and other 3rd party tools that will help you do this faster.

But, you can also do without them for this kind of specific tasks just by using VS.NET built in Find & Replace feature.

A sample class might looks like this...

namespace Sample
{
  public class Person
  {
     // Private Members
     private string firstName;
     private string lastName;

     :
  }
}

To create public properties for the private members (firstName, lastName), you can do the following...

1. Copy the private members lines to a blank line that you wish to “expand“ your public properties to and select them.

    private string firstName;
    private string lastName;

2. Open the Find & Replace dialog (Ctrl-H)

3. Enter this regular expression in the Find what: text box

    private {.+} {.+};

4. Enter this regular expression replace string in the Replace with: text box

    \npublic \1 \2\n\{\n\tget \{ return \2; \}\n\tset \{ \2 = value; \}\n\}\n

5. Make sure to check Use: Regular Expression in the Find options box.

6. Make sure to restrict your find to the current selection (Look in: Selection).

7. Click the Replace All button, and you should have your public properties expanded for you from the copy of private members statements.

You should have something like so...

public string firstName
{
    get { return firstName; }
    set { firstName = value; }
}

public string lastName
{
    get { return lastName; }
    set { lastName = value; }
}

8. From here, it's just some little corrective steps like Capitalizing the first letter of your properties. I.e. from firstName to FirstName, etc. and reformating the lines.

Automating parameter creation for your constructor is much simpler than the previous tips.  All you nee d to do is repeat step 1 and copy it to where your constructor parameter starts like so...

    public Person(
    private string firstName;
    private string lastName;
   
)
    {
}

and use the following regular expression to complete the process...

\1 \2,

and click the Replace All button.

You should have something like:

    public Person(
    string firstName,
    string lastName,
    )

From there just remove the last , from the last parameter and there you'll have your parameters for your constructor.

Again, you can do similar thing with the body of the constructor...

just use the following regular expression as the replace string:

this\.\2 = \2;

This will replace private string firstName; to this.firstName = firstName;

Of course with the new VS 2005, you won't have to do this anymore since you will have built in class designer and refactoring tools that will allow you to do this with ease.  In the mean time, your option is to get refactoring tools like Resharper or other third party tools or you can use this tip.

This tips can also be applied to any other code editor that support Regular Expression Find & Replace feature.

Happy coding!

 

Share this post: | | | |
Posted by Jimmy Chandra | 3 comment(s)
Filed under: ,

Enterprise Library Webcasts

I was just browsing the other day and came to this URL (www.pnplive.com).  If you are into Microsoft Application Blocks, you should look at the webcasts that they provided on this site (if not already).  Also available are the presentation slides, etc.

For those who hasn't already know, the major Microsoft Application Blocks have been combined into a more integrated solution called Enterprise Library.  To learn more about Enterprise Library, visit their workspace at http://www.gotdotnet.com/workspaces/workspace.aspx?id=295a464a-6072-4e25-94e2-91be63527327

More info can also be found at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/entlib.asp

 

Share this post: | | | |
Posted by Jimmy Chandra | 1 comment(s)
Filed under: ,