-1

How would I make one parent and one child and inherit the IEquatable function for the child? here is my code

public class Category : IEquatable<Category>
{
   public string _Name { get; set; }
   public string _Id { get; set;}

   private string _HomeCategory { get; set; }  
   public string _prestaId { get; set; }

   public bool _inUse { get; set; }

   public Category(string Name, string Id, string HomeCategory, bool inUse)
    {
        _Name = Name;
        _Id = Id;
        _HomeCategory = HomeCategory;
        _inUse = inUse;
    }

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

        if(this._Id == other._Id)
        {
            return true;
        }
        else { return false; }
    }
    
    public override int GetHashCode()
    {
        return this._Id.GetHashCode();
    }
}

public class SubCategory : Category :   
{
    public string parentCategory { get; set; }

    public SubCategory(string Name, string Id, string HomeCategory, bool inUse) : base(Name, Id, HomeCategory, inUse)
    {
    }
}

So I have to object and subcategory derives from category but contains method does not work with subcategory but it does with category. What am I doing wrong how would I derive the method of Category so that contains on a list with subcategory as its type would work?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

2 Answers2

0

It seems that you need to implement IEquatable<SubCategory> on your SubCategory. And SubCategory definition should change to public class SubCategory:Category, IEquatable<SubCategory>.

Without this you will have next behaviour:

var sub = new SubCategory { _Name = "1", parentCategory = "2" };
var x = new List<Category>();
x.Add(sub);
Console.WriteLine(x.Contains(new SubCategory
{
    _Name = sub._Name,
    parentCategory = sub.parentCategory
})); // prints "True"

var y = new List<SubCategory>();
y.Add(sub);
Console.WriteLine(y.Contains(new SubCategory
{
    _Name = sub._Name,
    parentCategory = sub.parentCategory
})); // prints "False"

And it makes sense, because SubCategory is IEquatable<Category> but is not IEquatable<SubCategory>, and IEnumerable.Contains docs state:

Elements are compared to the specified value by using the default equality comparer, Default.

Which internally uses the implementation of the IEquatable<T>.Equals in your class.

halfer
  • 19,824
  • 17
  • 99
  • 186
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
0

SubCategory does inherit bool Equals(Category other), but it would apply to any Category instance- meaning that a SubCategory and a Category would be "equal" if they have the same Id.

It sounds like you want an "automatic" implementation of IEquatable<SubCategory> and a bool Equals(SubCategory other) implementation, which is not possible. You'd need to explicitly add the interface declaration, but the implementation should be trivial:

public class SubCategory : Category , IEquatable<SubCategory>  
{
    public string parentCategory { get; set; }

    public SubCategory(string Name, string Id, string HomeCategory, bool inUse) : base(Name, Id, HomeCategory, inUse)
    {

    } 

    public bool Equals(SubCategory other)
    {
        return ((Category).this).Equals((Category)other)
    }
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240