0

I have problem in comparing two objects. I am struggling to find the difference between two objects. It gives me always same result.

Here is my object:

ClassA
{
    int slNo;
    string location;
    Dictionary dic = new Dictionary<string,Object>();
    ClassB classb = new ClassB();
}

ClassB
{
    int id;
    string name;
}

I have created an instance for classA and assigned some values.

ClassA obj = new ClassA{ dic = new Dictionary<string, object>, classb = new ClassB()};

and populated the obj with some values.

Copied the values to new object

var objNewA = obj;

and changed the classA like below.changed the value of obj.slNo = 100 and also changing some dic values collection. I can able to find the difference of slno using below method

 public static bool FindDifference(ClassA originalObject, ClassA changedObject)
 {
     foreach (PropertyInfo property in originalObject.GetType().GetProperties())
     {
         object originalValue = property.GetValue(originalObject, null);
         object newValue = property.GetValue(changedObject, null);

         if (!Equals(originalValue, newValue))
         {
              return true;
         }
     }
     return false;
 }

but the dictionary value change i cant able to figure it out. Its giving me the modified value in both source and destination objects any help please?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1845163
  • 307
  • 1
  • 5
  • 17

3 Answers3

1

Writing var objNewA = obj; doesn't copy values from your original object, they just get the same reference. You have then 2 variables accessing the same object in memory, and modifying obj also applies to objNewA. You'll never find any difference between them.

What you can do is cloning you object, like in this SO question. You'll get two objects with two different references but property values will be the same.

About checking dict, i don't think that Reflection is the best way to go, as it can quickly get complicated to write and to maintain. Plus, your parameters are of type ClassA, you already have access to your properties, without Reflection.

If you need more generic behavior about comparing two ClassA objects, what about overriding object.Equals in you class definition ? You're returning a boolean, which means that you only what to check if check values are equals. For example, you can add this in ClassA :

public override bool Equals(Object obj)
{
    if (obj == null || !(obj is ClassA))
       return false;
    else
    {
        ClassA other = (ClassA)obj;
        bool isEqual = // ... do all the checks you need, like comparing this.dict with other.dict
        return isEqual;
    }
}
Community
  • 1
  • 1
Réda Mattar
  • 4,361
  • 1
  • 18
  • 19
0

When comparing two objects, implement the IComparable interface.

How to Implement IComparable interface?

Community
  • 1
  • 1
eflles
  • 6,606
  • 11
  • 41
  • 55
0

you are actually trying to compare two equals object. The statement:

var objNewA = obj;

means "assign to objNewA the reference of obj", therefore any changes to obj will be present in objNewA. If you want to "clone" an object instance, your ClassA must implement ICloneable Interface, and call Clone() method on the object you want to clone.

In the end, since you're comparing two custom classes, you have to implement IEquatable interface in order to use Equals().
Once implemented you can do just

originalObject.Equals(changedObject);

in order to achieve you goal

Gianlucab
  • 1
  • 1