Click here to Skip to main content
13,193,551 members (50,079 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 20 Sep 2006

Swap Assignments Macro

, 20 Sep 2006
Rate this:
Please Sign up or sign in to vote.
A Visual Studio macro to swap assignments in source code


I find this useful and couldn't find anything like it before I wrote it, hence this article.

This is a simple little macro I came up with to alleviate the tedium of reversing assignments in source code. This is something I come across quite frequently when working with business objects and in ASP.NET forms where you initially set a series of objects with a value, then later want to reverse the assignment.

For example, you might want to convert this...; 

... to this:; 

Multiply that by dozens of assignments and it's quite tedious to reverse all that original code you wrote. Since this is very simple, I've just included it here in the article content. You can copy and paste it into your own macro in Visual Studio.

Note that it appends each line of code with a semicolon for C#, but you could easily modify it to not do that for VB or whatever you happen to be editing.

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports System.Text
Imports System.Text.RegularExpressions   

Public Module JohnUtils
    'Get currently selected text function
    Private Function GetCurrentlySelectedText() As String
        If Not DTE.ActiveDocument Is Nothing Then
            Dim txt As TextSelection
            txt = CType(DTE.ActiveDocument.Selection, TextSelection)
            Return txt.Text
            Return String.Empty
        End If
    End Function 
    'Swap assignments, go through each line in the currently selected text
    'and swap the left and right side and output as a block below the selected text
    Sub SwapAssignmentRegion()
        Dim line, originalCode As String
        originalCode = GetCurrentlySelectedText()
        If Not originalCode = String.Empty Then
            Dim leftside As String
            Dim rightside As String
            Dim output As New System.Text.StringBuilder
            Dim lines() As String
            lines = Split(originalCode, vbLf)
            Dim r As Regex
            'Use a regular expression to parse out the text on either side of
            'the equals sign in the line being processed
            r = New Regex( _
                "\s*(?<leftside>\S*)" & _
                "\s*(=)\s*(?<rightside>\S*)", _
                RegexOptions.IgnoreCase Or _
            For Each line In lines
                line = line.Trim
                If Not line = String.Empty Then
                    Dim mtch As Match
                    mtch = r.Match(line)
                    If mtch.Success Then
                        leftside = _
                        rightside = _
                          mtch.Groups("rightside").Value.Trim.Replace(";", "")
                        'Remove the semicolon replace statement above and 
                        'in the output format below for VB
                        output.AppendFormat( _
                               "{0}{1} = {2};", _
                             vbCrLf, rightside, _
                    End If
                End If
            'Output the swapped region with undo context
            'so user can undo the whole operation at once
            Dim txt As TextSelection
            txt = CType(DTE.ActiveDocument.Selection, TextSelection)
            txt.Insert(vbCrLf + output.ToString, _
                vsInsertFlags.vsInsertFlagsInsertAtEnd _
                Or vsInsertFlags.vsInsertFlagsContainNewText)
        End If 
    End Sub 
End Module


  • 20th September, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Member 96
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
amirhosseinhashemzadeh25-Oct-11 0:07
memberamirhosseinhashemzadeh25-Oct-11 0:07 
GeneralGreat! Pin
Vertexwahn4-Jan-10 11:25
memberVertexwahn4-Jan-10 11:25 
GeneralRe: Great! Pin
John C4-Jan-10 11:32
memberJohn C4-Jan-10 11:32 
GeneralGreat!! Pin
BigJoe71419-Feb-09 5:12
memberBigJoe71419-Feb-09 5:12 
GeneralMy vote of 1 Pin
Howard Richards29-Jan-09 22:09
memberHoward Richards29-Jan-09 22:09 
GeneralRe: My vote of 1 Pin
KramII22-Jul-09 6:22
memberKramII22-Jul-09 6:22 
GeneralRe: My vote of 1 Pin
John C22-Jul-09 7:57
memberJohn C22-Jul-09 7:57 
GeneralNice, but extend it Pin
PJ Arends20-Sep-06 13:46
memberPJ Arends20-Sep-06 13:46 
GeneralRe: Nice, but extend it Pin
John Cardinal20-Sep-06 14:04
memberJohn Cardinal20-Sep-06 14:04 
QuestionWhat about trying DataBinding? Pin
yesildal20-Sep-06 12:57
memberyesildal20-Sep-06 12:57 
AnswerRe: What about trying DataBinding? [modified] Pin
John Cardinal20-Sep-06 14:01
memberJohn Cardinal20-Sep-06 14:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171018.2 | Last Updated 20 Sep 2006
Article Copyright 2006 by Member 96
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid