TIP: A Quick Way to Write Public Properties in C#

Assume that you have a set of member variables declared as:

private string mAgencyName;
private string mInvoiceNumber;
private string mCustomerNumber;
private string mAddress1;
private string mAddress2;
private string mSuburb;
private string mPostcode;
private string mState;
private string mABN;

Just copy and paste this declaration block, highlight it, and press Control+H (find and replace). Ensure that regular expressions is turned on, and Applies to "Selection" is set. Type the following into the find and replace boxes respectively:

Find:

private {.+} m{[^ ]+};

Replace: (one line)

public \1 \2\n\t\t\{\n\t\t\tget \{ return m\2; \}\n\t\t\tset \
       { m\2 = value; \}\n\t\t\}\n

This will create public get and set commands for all of the variables. It should be fairly easy to manipulate the expressions above to work with your individual naming conventions.

That was the tip; now, what's it actually doing? Well, there's already a plethora of information on regular expressions on the net. If nothing else, this tip should show you some of what they can do. If you are interested in learning more about them, check out Microsoft's Regex Site at MSDN Regex Guide.

In this example, you start by finding the following string:

private {.+} m{[^ ]+};

This looks for the word "private" followed by a space. The {.+} tells the regex parser to look for any character (the .) one or more times (the +). The parentheses around it tell it to call this match Group 1.

The next part of the regex looks for a space followed by the letter "m" (assuming you have called your member variables mSomething). After this, you look for {[^ ]+}. Once again, you are defining a group—Group 2. You then are saying to look for any character in the following set [^ ]. Because the caret (^) is the first character, this tells regex to find any character NOT contained in the []; in this case, search for all charcters that are not a space. Once again, you want at least one character in this group. Finally, you look for a ";" to indicate the end of the line.

The replace syntax looks complicated, but is actually simpler than the find. It looks like this:

public \1 \2\n\t\t\{\n\t\t\tget \{ return m\2; \}\n\t\t\tset \
       { m\2 = value; \}\n\t\t\}\n

You are telling regex to replace what you have found with the string above. Regex will interpret escape sequences (\x) as characters or groups of characters. This is where your group definitions come in. Take the replace string above and change all of the escape sequences with the following:

  • \1: Group 1
  • \2: Group 2
  • \n: New line
  • \t: Tab
  • \{: { character (you need to put a \ in front of special characters so regex knows you want that character and not a group.
  • \}: } character

So, Group 1 becomes the variable type and Group 2 becomes the variable name.

Simple, huh?



Comments

  • Hello!

    Posted by wherlereeni on 10/23/2012 09:24pm

    Amazing! viagra livraison 24h france , +.99 for cialisis

    Reply
  • Nicely done

    Posted by medoz on 08/09/2005 07:36pm

    Though, I had to put on the search field just: "private {.+} m{[^ ]+};" and not " private {.+} m{[^ ]+};" as shown in the article (I guess the - - was just some HTML thing) But other than that, this tip is of great help :)

    • Thanks.

      Posted by pete# on 08/10/2005 01:17am

      Yeah it was a html thing ;) I have just submitted a revision (with a little background on regex) and removed the space from the search field.

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds