Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
I'm using a class for my equipemnts:

C#
public class EQUentity
    {
        public int ID_EQU { get; set; }
        public string CODE_EQU { get; set; }
        public string DESIGNATION_EQU { get; set; }
    }

then I create two entities:
C#
EQUentity EQU1 = new EQUentity 
EQUentity EQU2 = new EQUentity

EQU1.ID_EQU = ....;
...

EQU2.ID_EQU = ....;
......

Now how to control if the two entities are == or not?
Can you help me with a CLASS witch scans the elements of the entitiy to do this control?

Like this:

C#
public class compare(EQUentity E1, EQUentity E2)
    {
        .....
        ....
    }



Best regards
Posted
Updated 5-Sep-13 21:58pm
v4
Comments
Marvin Ma 6-Sep-13 3:51am    
Do you only want to compare if they're equal to each other?
nabilg 6-Sep-13 4:08am    
The ID_EQU remains the same but CODE and DESIGNATION can change; if one of these change, for me, they are not similar. I like to know else witch property changed.
I have several Kind of Entities (EQUentity, ART entity, ...) and would like to be able to use the same class to do this.
Thanks
phil.o 6-Sep-13 3:59am    
What is the logic of equality?
Are they equal if their IDs are equal, or are they equal if both CODE and DESIGNATION are equal? (ie, is it able for two entities with different IDs to have same designation and code?)

 
Share this answer
 
A little example for you:

C#
if(EQU1.ID_EQU.Equals(EQU2.ID_EQU))
{
    //Logic here
}
 
Share this answer
 
Use Object.Equals Method (Object) to compare to object in c#.

Go To Below Link, It will help you to understand Object.Equals method.

http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx

You have to override equals method in your class.

C#
public override bool Equals(object obj)
      {
          EQUentity equOBJ = obj as EQUentity;
          if (equOBJ == null)
          {
              return false;
          }
          else
          {
              if (ID_EQU == equOBJ.ID_EQU && CODE_EQU.Equals(equOBJ.CODE_EQU) && DESIGNATION_EQU.Equals(equOBJ.DESIGNATION_EQU))
              {
                  return true;
              }
              else
              {
                  return false;
              }
          }
      }
 
Share this answer
 
Comments
nabilg 6-Sep-13 4:11am    
The ID_EQU remains the same but CODE and DESIGNATION can change; if one of these change, for me, they are not similar. I like to know else witch property changed. I have several Kind of Entities (EQUentity, ART entity, ...) and would like to be able to use the same class to do this. Thanks
 
Share this answer
 
Comments
nabilg 6-Sep-13 6:39am    
Thank you. That exactly what i was looking for!
Gitanjali Singh 6-Sep-13 10:17am    
Welcome
Another solution would be to implement the IEquatable<EQUentity> and IEquatable interfaces.

Thus:
C#
using System.Collections.Generic
public partial class EQUentity : IEquatable<EQUentity>, IEquatable
{
   public int ID_EQU { get; set; }
   public string CODE_EQU { get; set; }
   public string DESIGNATION_EQU { get; set; }

   public bool Equals(EQUentity other)
   {
      // Logic to determine whether this and other are equal or not
   }

   override bool Equals(object obj)
   {
      if (!obj is EQUentity) {
         return false;
      }

      if (this.ReferenceEquals(obj)) {
         return true;
      }

      return this.Equals((EQUentity)obj);
   }
}


Here is the idea. The only thing that remains to be done is to define the logic to determine whether both instances are equal or not.

And the cherry on the cake : you can even define == and != operators to be able to write:
C#
if (leftEntity == rightEntity)


This way:
C#
public static bool operator ==(EQUentity left, EQUentity right)
{
   return left.Equals(right);
}

public static bool operator !=(EQUentity left, EQUentity right)
{
   return !left.Equals(right);
}


Hope this helps.
 
Share this answer
 
v6

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900