Reputation: 1
I am implementing a binary tree using the following code:
class Binary<T>:Binary
where T: IComparable
{
But I'm also looking to use class objects in the tree:
Binary Bin = null;
Bin = new Binary<Product>();
This obviously doesnt work because Product
is not IComparable
. I tried doing this to make it work:
public class Product: IComparable<Product>
{
public int Id = 0;
public int CompareTo(Product p)
{
if (p==null)
{
return 1;
}
if (p!=null)
{
return this.Id.CompareTo(p.Id);
}
return 0;
}
Id
is the value of Product
intended to be used in all comparisons. But this doesn't work and I dont know what else to do. Is it possible to do what i want? Or is there another way I can use the class Product
as an IComparable
?
Upvotes: 0
Views: 761
Reputation: 125610
Your class Product
implements generic IComparable<T>
, where your generic class Binary<T>
expects generic type that implements non-generic IComparable
.
Update Binary<T>
declaration to useIComparable<T>
in the constraint:
class Binary<T>:Binary
where T: IComparable<T>
{
Upvotes: 3