tags:

views:

163

answers:

5

I know this might be trivial to some but when programming say in c# and you have a very large data structure. I usually do assignment via equation for setting value in control and then later do it the reverse way.

Control1.Text = data.value1;
Control2.SelectedValue = data.value2;

Reverse:

data.value1 = Control1.Text;
data.value2 = Control2.SelectedValue;

I was wondering if there is a tool to do this quickly. This is to assume that you have a very large set of values.

+1  A: 

Good old excel comes to rescue.

Paste the statement into cell A1.

Goto cell B1 & paste this formula -
=MID(A1,1,SEARCH(";",A1) - 1)

Goto cell C1 & paste this formula -

=CONCATENATE(TRIM(MID(B1,SEARCH("=",B1)+1, 100)),
   " = ", 
   TRIM(LEFT(B1,SEARCH("=",B1)-1)), 
   ";")

Paste each of your statement in a separate cell.
cell A1: textBox1.Value = textBox2.Value;
cell A2: textBox2.Value = textBox4.Value;

It relies on a the assumption that the statement ends with semicolon.
At least, it gets you what you need.

shahkalpesh
...Joel?     
Robert Venables
shahkalpesh
This would be difficult if you have a program already in c#. It would mean you need to export the data with space as delimiter to import the data to excel...
Nassign
+2  A: 

ReSharper 4.5 supports this; select the lines, hit Alt+Enter, and choose Reverse assignments.

TrueWill
while I agree with the uses of resharper, this reminds me of "when you look at nail, you would want to have a hammer" :)
shahkalpesh
+8  A: 

You could use Visual Studio Find & Replace to perform the swap. Here's a regular expression pair that will perform the replacement automatically:

Find: ^{:b*}{([^=]+)} += +{([^=]+)};
Replace: \1\3 = \2;

Remember to turn on regular expressions. This will do exactly what you are asking for. This can also be encapsulated into a macro. Here's an example Macro that I put together:

Sub SwapAssignments()
    DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
    DTE.Find.FindWhat = "^{:b*}{([^=]+)} += +{([^=]+)};"
    DTE.Find.ReplaceWith = "\1\3 = \2;"
    DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocumentFunction
    DTE.Find.MatchCase = False
    DTE.Find.MatchWholeWord = False
    DTE.Find.MatchInHiddenText = True
    DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
    DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
    If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
        Throw New System.Exception("vsFindResultNotFound")
    End If
    DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close()
End Sub

...This will simply swap assignments in the current block.

Robert Venables
This is what I am exactly looking for. I mean the regular expression. I am not that well verse in regular expressions. Maybe you can explain the meaning of the Find what each characters do. Maybe a brief explanation could help the next person that would find this post.
Nassign
Absolutely. I'll break the Find section into pieces an explain what each does. ^{:b*} says "Start at the beginning "^" of the line, and capture "{}" zero or more "*" space or tabs ":b". Since this is the first capture, it is stored in the first index "\1". This was included to preserve indenting but you could probably accomplish the same result some other way. Next, we have {([^=]+)} which says match one or more "+" sequential characters that are not equals signs "[^=]" and capture "{}" the match. Then I match one or more spaces " +", an equal sign, and one or more spaces " +" again...
Robert Venables
...The last block {([^=]+)}; simply works the same way to capture a sequential string of characters that do not contain an equal sign and are immediately followed by a semicolon. The Replace section simply takes the captured strings "{...}" and reorders them. Read the replace expression as "Capture one (any indenting) followed by capture three (second identifier in original assignment expression), an equals sign, and capture two (first identifier in original assignment expression).
Robert Venables
+1  A: 

Found this too: another example, and explanation on how to install it:

http://www.switchonthecode.com/tutorials/how-to-configure-and-use-visual-studio-macros

EJB
A: 

I also saw a tool named MZTools that has a swap assignment in the screenshot. alt text

Nassign