Inverting Assignment Operations

I found that frequently (particularly when working with dialogs) I was doing a bunch of assignment operations, performing some other operation, then doing the reverse of those same assignment operations. The following is a typical example:
	ClientSheet.m_lClientNo     = m_ClientNo;
	ClientSheet.m_szCompanyName = m_CompanyName;
	ClientSheet.m_szEmail       = m_Email;
	ClientSheet.m_szFirstName   = m_FirstName;
	ClientSheet.m_szLastName    = m_LastName;
	ClientSheet.m_szTitle       = m_Title;

	if (ClientSheet.DoModal()==IDOK) {
		m_ClientNo    = ClientSheet.m_lClientNo;
		m_CompanyName = ClientSheet.m_szCompanyName;
		m_Email       = ClientSheet.m_szEmail;
		m_FirstName   = ClientSheet.m_szFirstName;
		m_LastName    = ClientSheet.m_szLastName;
		m_Title       = ClientSheet.m_szTitle;
	}

Instead of writing the same code twice, I developed this macro to apply to the code. It will act on the current line, or more then one selected lines. If there is no assignment operator in the line, then that line is ignored. The macro treats everything after the first equal sign as the second operand, so if it's applied to something like a=b=c, you'll get b=c=a.

	Sub Invert()
	'DESCRIPTION: Invert an assignment operation
		Dim win
		set win = ActiveWindow
		if win.type <> "Text" Then
			MsgBox "You can only run this macro when a text editor window is active."
		else
			StartLine = ActiveDocument.Selection.TopLine
			EndLine = ActiveDocument.Selection.BottomLine
			If EndLine < StartLine Then
				Temp = StartLine
				StartLine = EndLine
				EndLine = Temp
			End If

			For i = StartLine To EndLine
				TmpBlock = ""

				ActiveDocument.Selection.GoToLine i
				ActiveDocument.Selection.StartOfLine dsFirstText
				ActiveDocument.Selection.EndOfLine dsExtend
				CmtBlock = ActiveDocument.Selection

				Trim(CmtBlock)

				equal = Instr(CmtBlock,"=")
				semi = Instr(CmtBlock, ";")
				If equal <> 0 Then 
					TmpBlock = Left(CmtBlock, equal-1)
					If semi <> 0 then
						CmtBlock = Mid(CmtBlock, equal + 1, semi-equal-1)
					else
						CmtBlock = Right(CmtBlock, Len(CmtBlock) - equal)
					End If

					CmtBlock = Trim(CmtBlock)
					TmpBlock = Trim(TmpBlock)

					if semi <> 0 then
						CmtBlock = CmtBlock + " = " + TmpBlock + ";"
					else
						CmtBlock = CmtBlock + " = " + TmpBlock
					End If
				End If
				ActiveDocument.Selection = CmtBlock
			Next
		End If
	End Sub

Last updated: 2 April 1998



Comments

  • Very nice macro

    Posted by Legacy on 06/08/1999 12:00am

    Originally posted by: Player

    Thanks for a great macro. A real time saver.

    Reply
  • should be using classes

    Posted by Legacy on 10/19/1998 12:00am

    Originally posted by: Mike

    Your macro looks pretty cool... but one blatant thing comes
    
    to mind when looking at it...

    instead of this:
    ClientSheet.m_lClientNo = m_ClientNo;
    ClientSheet.m_szCompanyName = m_CompanyName;
    ClientSheet.m_szEmail = m_Email;
    ClientSheet.m_szFirstName = m_FirstName;
    ClientSheet.m_szLastName = m_LastName;
    ClientSheet.m_szTitle = m_Title;


    why not this:

    class X
    {
    int m_ClientNo;
    CString m_CompanyName;
    CString m_Email;
    CString m_FirstName;
    CString m_LastName;
    CString m_Title;
    }

    class ClientSheet
    {
    X worksaver;
    }

    now in your dialog class:
    class somedlg
    {
    X dlgData;
    }


    now when you use the dlg class...
    somedlg.dlgData = ClientSheet.worksaver;
    if( dlg.DoModal == IDOK )
    {
    ClientSheet.worksaver = dlgData;
    }

    AHHHH much better.... I can breath again....

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds