-1

I have two self-made objects. PersonalizedCard and BaseCard. I want to compare these two for equality.

The PersonalizedCard inherits BaseCard class.

BaseCard has Color and Number.

PersonalizedCard has Color, Number and Name

(Both of them have at least 20 different variables in them, which I left out for clarity)

I tried casting them both back and forth to check for equality, but haven't found a proper solution yet.

var oldCard = UserCards.Cast<IUserCardObject>().FirstOrDefault((card) => card.PersonalCardObject == CurrentCard);

Here CurrentCard is a BaseCard and I'm comparing it to card.PersonalCardObject which inherits the BaseCard class.

I want to compare BaseCard and PersonalizedCard on Color and Number. If they're both a BaseCard, this should be possible I feel.

How do I do this?

Zen Zac
  • 136
  • 1
  • 9
  • So are your 20 different variables defined on the BaseCard OR in BaseCard and PersonalizedCard separatedly? How do those different properties map to each other if latter case? – Janne Matikainen Oct 15 '19 at 07:05
  • They're defined in BaseCard and PersonalizedCard inherits them. PersonalizedCard only has a few extra variables that are not on BaseCard. – Zen Zac Oct 15 '19 at 07:06
  • Then overriding the Equals method and checking all properties related to equality is necessary. – Janne Matikainen Oct 15 '19 at 07:07
  • Most everything you need to know about overriding `Equals()` can be found in the marked duplicate. Including the pros/cons of allowing instances of two different types to be considered equal to each other, as you're proposing here (hint: it's almost never a good idea). I would recommend instead you consider implementing `IEqualityComparer`, or even just using a `Comparison` delegate, depending on the context. – Peter Duniho Oct 15 '19 at 07:14

1 Answers1

2

Obj1 == Obj2 will compare the reference, not the content.

If you want to have some custom comparison between 2 objects, you can implement the interface IEquatable

public bool Equals(BaseCard other)
{
    if (other == null)
        return false;

    /*
     * Add as many property comparison as needed
     * This will define what makes 2 objects equal
     */
    return this.Color == other.Color
           && this.Number == other.Number;
}

 public override bool Equals(Object obj)
 {
     if (obj == null)
         return false;

     BaseCard cardOther = obj as BaseCard;
     if (cardOther == null)
         return false;
     else
         return Equals(cardOther);
}

public override int GetHashCode()
{
    return this.Number; // could be something else
}

/*
 * This allows you to do card.PersonalCardObject == CurrentCard
 * without comparing references
 * This is optional and not necessarily wanted
 */
public static bool operator == (BaseCard card1, BaseCard card2)
{
    if (((object)card1) == null || ((object)card2) == null)
        return Object.Equals(card1, card2);

    return card1.Equals(card2);
}

// Same as above, this is optional and not necessarily wanted
public static bool operator != (BaseCard card1, BaseCard card2)
{
    if (((object)card1) == null || ((object)card2) == null)
        return ! Object.Equals(card1, card2);

    return ! (card1.Equals(card2));
}
Cid
  • 14,968
  • 4
  • 30
  • 45