Click here to Skip to main content
6,629,885 members and growing! (23,439 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Diff     Intermediate License: The Code Project Open License (CPOL)

Compare objects for any change in value

By Vikramaditya S Shekhawat

Compare objects members to the deepest thread for any value change.
C#, .NET, Architect, Dev
Posted:11 Nov 2008
Views:4,234
Bookmarked:7 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
16 votes for this article.
Popularity: 2.49 Rating: 2.07 out of 5
10 votes, 62.5%
1
1 vote, 6.3%
2
3 votes, 18.8%
3

4
2 votes, 12.5%
5

Introduction

Object comparision is always been a topic of debate. There are lot of ways to implement it, using comparer interface and implement it etc. But there is no generic way, by which two similar object of any type/class can be compared for the change in value of any of the memebers.

Using the Code

Just implement below mentioned methods in a class of your own.

Blocks of code should be set as style "Formatted" like this:

private static string CompareObjects(object initialObj, object currentObj)
{
    string returnMessage = string.Empty;

    Type type = initialObj.GetType();
    Type type2 = currentObj.GetType();
    PropertyInfo[] propListInfo = type.GetProperties();
    PropertyInfo[] propListInfo1 = type2.GetProperties();

    //if class type is native i.e. string,int boolean etc
    if (type.IsSealed == true && type.IsGenericType == false)
    {
        if (!initialObj.Equals(currentObj))
        {
            returnMessage = "initialObj value :{" + initialObj.ToString() + "},
                currentObj value :{" + currentObj.ToString() + "}";
        }
    }
    else //clas type is object
    {
        //loop through each property of object
        for (int count = 0; count < propListInfo.Length; count++)
        {
            //check if its a indexed object -- special case
            if (propListInfo[count].GetGetMethod().GetParameters().Length > 0)
            {
                // check if  not dict object --dict object needs to be addressed
                if (count > 1 && type.Name.IndexOf("Dictionary") == -1)
                {
                    int indexersTotal = 0;
                    //get index of count
                    for (int tmpCount = 0; tmpCount < propListInfo.Length; tmpCount++)
                    {
                        if (propListInfo[tmpCount].Name == "Count")
                        {

                            indexersTotal = (int)propListInfo[tmpCount].GetValue(
                                initialObj, null);


                            break;
                        }
                    }
                    //indexersTotal = (int)propListInfo[count - 1].GetValue(initialObj, null);
                    try
                    {
                        for (int curIndex = 0; curIndex < indexersTotal; curIndex++)
                        {
                            object Obj1 = propListInfo[count].GetValue(initialObj,
                                new object[] { curIndex });
                            object Obj2 = propListInfo1[count].GetValue(currentObj,
                                new object[] { curIndex });
                            returnMessage = Compare(Obj1, Obj2);
                            if (returnMessage.Length > 0)
                            {
                                return returnMessage + " " + propListInfo[count].ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //consume this error as it is occured becuase of dictionary
                        //setting collection
                        string str = ex.Message;
                    }
                }
            }
            else //not a indexed object
            {
                object Obj1 = propListInfo[count].GetValue(initialObj, null);
                object Obj2 = propListInfo1[count].GetValue(currentObj, null);
                returnMessage = Compare(Obj1, Obj2);
                if (returnMessage.Length > 0)
                {
                    return returnMessage + " " + propListInfo[count].ToString();
                }
            }
        }
    }


    public static string Compare(object initialObj, object currentObj)
    {
        string returnMessage = "";
        if (initialObj == null && currentObj != null)
        {
            returnMessage = "initialObj is Null Object";
        }
        else if (initialObj != null && currentObj == null)
        {
            returnMessage = "currentObj is Null Object";
        }
        else if (initialObj != null && currentObj != null)
        {
            returnMessage = CompareObjects(initialObj, currentObj);
        }
        return returnMessage;
    }

License

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

About the Author

Vikramaditya S Shekhawat


Member
Originally from Jaipur(Rajasthan).
Working in Norway for a Software company.Holding diploma from CDAC and MCSD.NET (C#).
Occupation: Architect
Location: India India

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
QuestionHow to compare html files and show diffrences? Pinmembernidjain16:27 23 Aug '09  
GeneralMy vote of 1 PinmemberJeffrey Schaefer9:06 16 Jun '09  
GeneralCompare .NET Objects PinmemberGreg Finzer7:30 1 Jun '09  
QuestionTest code properly before submitting. PinmemberMS_Jocker17:35 11 Mar '09  
GeneralFaulty logic PinmemberMKauffman7:34 11 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Nov 2008
Editor: Sean Ewington
Copyright 2008 by Vikramaditya S Shekhawat
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project