Geeks With Blogs
Darren Fieldhouse

One of the blogs I read is Zain Naboulsi’s Visual Studio Tips and Tricks. In a recent post he discussed the power of using Find and Replace with regular expressions and it gave me an idea about how to quickly reverse a set of assignment statements.

Lets say you have the following code:

Me.FirstNameTextBox.Text = customer.FirstName
Me.LastNameTextBox.Text = customer.LastName
Me.TelephoneTextBox.Text = customer.Telephone
Me.EmailTextBox.Text = customer.Email 

And you want to reverse the assignments so that you are writing the textboxes back into the customer object (generally the next thing you would want to do). Rather than doing it by hand, or using some macro, you can use the find and replace window with a regular expression:

FindAndReplace

This works because the expression matches any character (.) zero or more times (*), followed by an equals sign then any character zero or more times. In other words, anything followed by “=” followed by anything. The two anythings in the expression are enclosed with curly brackets to make them tagged expressions, which means they can be used in the replace expression. \1 will be the first tagged expression, \2 then next and so on. So our replace with expression just says replace a=b with b=a.

So what do you end up with? Well, this of course:

customer.FirstName = Me.FirstNameTextBox.Text
customer.LastName = Me.LastNameTextBox.Text
customer.Telephone = Me.TelephoneTextBox.Text
customer.Email = Me.EmailTextBox.Text

kick it on DotNetKicks.com Posted on Wednesday, September 15, 2010 4:34 AM | Back to top


Comments on this post: Reversing assignment statements with Find and Replace in VS 2010

# re: Reversing assignment statements with Find and Replace in VS 2010
Requesting Gravatar...
Awesome tip thank you. I have never actually thought about using that before, but after I read your post it completely made sense to do it. Like you say if you are doing any type of Automapper stuff, or reassignment statements this can become quite tedious!
Left by Gary Woodfine on Sep 15, 2010 3:57 PM

# re: Reversing assignment statements with Find and Replace in VS 2010
Requesting Gravatar...
Resharper has a reverse assignment quickfix.
Left by Paul Batum on Sep 23, 2010 4:16 PM

# re: Reversing assignment statements with Find and Replace in VS 2010
Requesting Gravatar...
There are loads of tools to do this, including many macros, but I really liked this light weight, build-in solution.
Left by Darren Fieldhouse on Sep 24, 2010 3:46 AM

# re: Reversing assignment statements with Find and Replace in VS 2010
Requesting Gravatar...
Handy if you don't have Resharper :-)
Left by Leyu Sisay on Sep 24, 2010 4:58 AM

# re: Reversing assignment statements with Find and Replace in VS 2010
Requesting Gravatar...
if you're in c# it should be more like this:

{.*}={.*};

\2 = \1;

That will take care of the semicolons.
Left by Don on Sep 27, 2011 11:21 AM

Your comment:
 (will show your gravatar)


Copyright © DarrenFieldhouse | Powered by: GeeksWithBlogs.net | Join free