Ich habe ein Objekt namens Shape
, das ein Feld public int[,] coordinate { get; set; }
enthält.IComparable in C#
Ich habe eine separate Klasse, die eine Sammlung von Shape
Objekte hat. An einem bestimmten Punkt möchte ich überprüfen:
if(shapes.Contains(shape))
{
// DoSomething
}
So in der Shape
Klasse habe ich hinzugefügt, um die IComparable
Referenz und eingefügt, um die CompareTo
Methode:
public int CompareTo(Shape other)
{
return this.coordinate.Equals(other.coordinate);
}
ich aber eine Störung erhalte:
Cannot implicitly convert type 'bool' to 'int'
Wie schreibe ich daher die Rückkehr, so dass es ein int zurückgibt und nicht ein bool, wie es im Moment so ist?
UPDATE
Wenn ich den Return-Code zu ändern:
return this.coordinate.CompareTo(other.coordinate);
bekomme ich folgende Fehler mesage:
Error 1 'ShapeD.Game_Objects.Shape' does not implement interface member 'System.IComparable.CompareTo(ShapeD.Game_Objects.Shape)'. 'ShapeD.Game_Objects.Shape.CompareTo(ShapeD.Game_Objects.Shape)' cannot implement 'System.IComparable.CompareTo(ShapeD.Game_Objects.Shape)' because it does not have the matching return type of 'int'. C:\Users\Usmaan\Documents\Visual Studio 2012\Projects\ShapeD\ShapeD\ShapeD\Game Objects\Shape.cs 10 18 ShapeD
Die neue Fehlermeldung ist fair einfach zu lösen, ändern Sie ** public int CompareTo (andere Form) ** zu ** public int CompareTo (Objekt andere) **, aber dann werden Sie Casting-Probleme auftreten und CompareTo existiert nicht für multidimensionale Arrays. –