Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; 
like image 261
ckal Avatar asked Jan 09 '09 23:01

ckal


1 Answers

Before VS2012:

  • 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

With VS2012 (and presumably later) which uses .NET regular expressions:

  • 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};
  • Make sure that the .* (regular expressions) icon is selected (the third one along under the replacement textbox)
  • Hit Replace All
like image 131
Jon Skeet Avatar answered Oct 11 '22 19:10

Jon Skeet