Click here to Skip to main content
Click here to Skip to main content

Comparison and synchronization tool using C#

By , 10 Nov 2005
Rate this:
Please Sign up or sign in to vote.

Introduction

This article introduces a compare/synchronize tool written using C#. The control was initially created for the synchronization of two tables from different databases (production and testing databases). However, I noticed that it could be extended for comparison and synchronization of any object types. The demo presented in the article is used for comparing and synchronizing string based key-value pairs.

Design

The control’s structure is presented in the following figure:

The control receives two collections of type CompareCollection as input:

public void DisplayCollections ( CompareCollection A, 
                                     CompareCollection B )

The CompareCollection implements ICollection interface and contains objects of type CompareItem. The CompareItem class contains the following members:

private object      m_sKey;
private object      m_sValue;
private object      m_sNewKey;
private object      m_sNewValue;        
private bool        m_bWasAdded         = false;
private bool        m_bWasModified      = false;
private bool        m_bWasDeleted       = false;
private bool        m_bIsDifferentKey   = false; 
private bool        m_bIsDifferentValue = false; 
private bool        m_bIsDummy          = false;
private bool        m_bIsReadOnly       = false;

Key is an essential member of the class. Key is used for the placement of an object in a ListBox. For instance: Two string objects will be displayed in the same line of both ListBoxes if and only if both of them have the same keys. (In case, items' values are different, lines containing these items will be highlighted.)

Most of the flags are self explanatory except for IsDifferentKey and IsDifferentValue. Note that iCompareKey and iCompareValue are CompareItem's virtual functions and should be overridden for appropriate objects. In this example, both the key and value are casted to the string type for comparing.

Another virtual parameter of the class is sDisplayString which is used for displaying a string in the ListBox. Majority of the logic is concentrated in the LineComparatorControl class. The main functions are:

  • DoDisplayCollections: The function places items in the ListBox:
    private void DoDisplayCollections ( 
              CompareCollection oLeft, CompareCollection oRight ) {
                
         int iLeftIndex  = 0;
         int iRightIndex = 0;
         int iSyncIndex  = 0;
         while ( iLeftIndex < oLeft.Count || iRightIndex < oRight.Count ){
             // Compare 
             int iCompResult = 0;
             if ( iLeftIndex == oLeft.Count ) {
                 iCompResult = 1;
             }else if ( iRightIndex == oRight.Count ){
                 iCompResult = -1;
             }else{
                 iCompResult =  oLeft[iLeftIndex].iCompareKey(
                                 oRight[iRightIndex] );
             }
    
             // Print List Box
             if ( iCompResult < 0  ){
                 // Print Left Side Only    
                 oLeft[iLeftIndex].bDifferentKey = true;             
                 LeftListBox.Items.Insert (iSyncIndex, oLeft[iLeftIndex]);
                 RightListBox.Items.Insert (iSyncIndex,                   
                                     new CompareItem( true, true, true ) );
                 iLeftIndex++;
             }else if ( iCompResult > 0 ){
                 // Print Right Side Only 
                 oRight[iRightIndex].bDifferentKey = true;
                 RightListBox.Items.Insert (iSyncIndex,
                                            oRight[iRightIndex] );
                 LeftListBox.Items.Insert (iSyncIndex, 
                                     new CompareItem( true, true, true ) ); 
                 iRightIndex++;
             }else{
               if (oLeft[iLeftIndex].iCompareValue(oRight[iRightIndex]) != 0 ){
                  oRight[iRightIndex].bDifferentValue = true;
                  oLeft[iLeftIndex].bDifferentValue = true;
               }
               RightListBox.Items.Insert ( iSyncIndex, 
                                           oRight[iRightIndex] );
               LeftListBox.Items.Insert ( iSyncIndex, 
                                          oLeft[iLeftIndex] );
               iLeftIndex++;
               iRightIndex++;
             }   
             iSyncIndex++;
         }
    }
  • bExecuteSync: This function is used for the synchronization of items.

    In case of synchronization failure, the function pops up a message box with an error message. In order to check whether synchronization is possible, a virtual function bIsSynchronizationOk is used.

    private bool bExecuteSync ( CompareItem oFromItem, 
        CompareItem oToItem, 
        ref string sErrorMsg ){
                
         if ( oFromItem.bDummy ){
             oToItem.bDeleted = true;                  
         }else if ( oToItem.bDummy ){
             oToItem.bAdded = true;
             oToItem.sNewKey = oFromItem.sKey;
             oToItem.sNewValue = oFromItem.sValue;
         }else {
             oToItem.bModified = true;
             oToItem.sNewKey = oFromItem.sKey;
             oToItem.sNewValue = oFromItem.sValue;
         }
         
         if ( bIsSynchronizationOk ( oToItem, ref sErrorMsg ) ){
             if ( !oToItem.bDeleted ){
                 oToItem.sKey = oToItem.sNewKey;
                 oToItem.sValue = oToItem.sNewValue;
             }
             if ( oToItem.bDeleted || oToItem.bAdded ){
                 oToItem.bDummy = false;
                 oFromItem.bDummy = false;
             }
             
             oToItem.bDifferentKey = false;
             oToItem.bDifferentValue = false;
             oFromItem.bDifferentKey = false;
             oFromItem.bDifferentValue = false;
             
             return true;
    
         }else{
             
             return false;
         }
    }

One would need to override functions LeftStoreButton_Click and RightStoreButton_Click in order to implement your own store logic for changed items.

Hope this article is helpful.

References

In order to highlight lines in the ListBox, I used the article: A C# ColorListBox.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Glaxalg
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
Generalsynchronization of multiple frames Pinmembernayanaps26-Apr-07 23:06 
I am having an application which will generate 2 frames of the same look and feel.These frames are to be displayed in two different monitors. Suppose i am having one checkbox in each frame. When i enable chechbox in one frame it should be checked in the other one also. Can anyone give me any logic regarding this.
 
Thanks in advance
Nayana

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 10 Nov 2005
Article Copyright 2005 by Glaxalg
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid