Reputation: 71
Error 1 'Koordinates.Vieta' does not implement interface member 'System.IComparable.CompareTo(object)'
the code:
public class Vieta : IComparable //error is thrown here, on "Vieta"
{
public double x, y;
//constructor
public Vieta(double a, double b)
{
x = a; y = b;
}
//read only parameter
public double ilgis
{
get
{
return Math.Sqrt(x * x + y * y);
}
}
public int CompareTo(Vieta other)
{
if (other == null) return 1;
return other.ilgis.CompareTo(this.ilgis);
}
}
Upvotes: 1
Views: 4515
Reputation: 56934
You have a compare method that takes an instance of the Vieta
class.
The IComparable
interface declares a method CompareTo(object)
(note that the parameter should of type object
).
It is offcourse better to have a strong typed CompareTo method. You could implement the IComparable<T>
interface as well.
My suggestion would be to do that, and implement the IComparable
interface explicitly. Then, you'll end up with something like this:
public class Vieta : IComparable, IComparable<Vieta>
{
IComparable.CompareTo( object obj )
{
var other = obj as Vieta;
if( other == null ) return false;
return CompareTo(other);
}
public int CompareTo( Vieta other )
{
// Implement your compare logic here.
}
}
By implementing the IComparable
interface explicitly, the CompareTo(object)
method will be hidden from intellisense. In fact, you'll only be able to call that method if you cast the class to the interface type (or if an instance of that class is passed to a method that takes an IComparable
parameter, for instance).
Upvotes: 2
Reputation: 1075
Change the class to this:
public class Vieta : IComparable<Vieta>
IComparable
defines an int CompareTo(object)
while the generic IComparable<T>
defines an int CompareTo(T)
.
Upvotes: 2