'How can I reverse code around an equal sign in Visual Studio?
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;
Solution 1:[1]
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
Solution 2:[2]
I had the same need but I had to accept more characters than a-zA-Z\. in the solution provided by John so I slightly modified its regular exception like this :
Find what : {^[^\=]*} = {.*}
Replace with : \2 = \1
This will reverse anything arround the first equal sign found on a line
Solution 3:[3]
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.
Solution 4:[4]
The Search term RegEx that worked for me:
Search
([^= ]*) = ([^= ]*);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marc Gravell |
| Solution 2 | Arno 2501 |
| Solution 3 | toad |
| Solution 4 | tinmac |
