views:

323

answers:

3

After writing code to populate textboxes from an object, such as:

txtFirstName.Text = customer.FirstName;
txtLastName.Text = customer.LastName;
txtAddress.Text = customer.Address;
txtCity.Text = customer.City;

is there way in Visual Studio (or even something like Resharper) to copy and paste this code into a save function and reverse the code around the equal sign, so that it will look like:

customer.FirstName = txtFirstName.Text;
customer.LastName = txtLastName.Text;
customer.Address = txtAddress.Text;
customer.City = txtCity.Text;
+2  A: 

None that I know of. Of course, if you use one of the many binding approaches available, then you won't have to - the binding will do the update in both directions (including change via notifications).

So for winforms:

txtFirstName.DataBindings.Add("Text", customer, "FirstName");

etc

Marc Gravell
+19  A: 
  • Copy and paste the original block of code
  • Select it again in the place you want to switch
  • Press Ctrl-H to get the "Replace" box up
  • Under "Find what" put: {[a-zA-Z\.]*} = {[a-zA-Z\.]*};
  • Under "Replace with" put: \2 = \1;
  • Look in: "Selection"
  • Use: "Regular expressions"
  • Hit Replace All
Jon Skeet
I use this in case there are digits in the values:{[^:b]*} = {[^:b]*};\2 = \1;
DavGarcia
skeet does it again....
Tom Anderson
wow, great solution.
Sara Chipps
Step 2: Create a Macro Step 3:Profit
Daniel
I like to throw in an underscore as well - {[a-zA-Z\._]*}
Kevin Pullin
A: 

An option to get them in there that way in the first place with Resharper would be to define a live template similar to:

$uiElement$ = $dto$;
$dto$ = $uiElement$;

This will allow you to type them once and it will duplicate it for you and then you can cut and paste the save version to the other method.

Jeremy Wilde