Inverting Assignment Operations | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 7, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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 <>TextThen
			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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.