質問

I have an object called Shape which contains a public int[,] coordinate { get; set; } field.

I have a separate class which has a collection of Shape objects. At a particular point, I wish to check:

if(shapes.Contains(shape))
{
   // DoSomething
}

So in the Shape class I have added the IComparable reference and inserted the CompareTo method:

public int CompareTo(Shape other)
{
    return this.coordinate.Equals(other.coordinate);
}

I am however getting an error:

Cannot implicitly convert type 'bool' to 'int'

How do I therefore phrase the return so that it returns an int and not a bool as it is doing so at the moment?

UPDATE

If I change the return code to:

return this.coordinate.CompareTo(other.coordinate);

I get the following error 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

役に立ちましたか?

解決

IComparable implies, that two object can be compared in a sense, that you can tell which object has "higher value". It is generally used for sorting purposes. You should override Equals method instead .You should also use Point struct instead of array.

class Shape : IEquatable<Shape>
{
    public Point coordinate { get; set; }

    public bool Equals(Shape other)
    {
        if (other == null) return false;
        return coordinate.Equals(other.coordinate);
    }

    public override bool Equals(object other)
    {
        if (other == null) return false;
        if (ReferenceEquals(this, other)) return true;
        var shape = other as Shape;
        return Equals(shape);
    }

    public override int GetHashCode()
    {
        return coordinate.GetHashCode()
    }
}

他のヒント

Since you only want to check for equality implement IEquatable interface not IComparable. IComparable is used for sorting purpose

public bool Equals(Shape s)
{

    int count=0;
    int[] temp1=new int[this.coordinate.Length];
    foreach(int x in this.coordinate)temp1[count++]=x;//convert to single dimention

    count=0;
    int[] temp2=new int[s.coordinate.Length];
    foreach(int x in s.coordinate)temp2[count++]=x;//convert to single dimention

    return temp1.SequenceEqual(temp2);//check if they are equal

}

NOTE

IEquatable should be implemented for any object that might be stored in a generic collection else you would have to also override Object's Equals method.Also as pointed out in other ans use Point struct instead of multidimentional array

For performing Contains check you need to override Equals operator in Shape class.

Resurrecting an old question only because it can still cause google hits despite some really poor answers. You should not be using either CompareTo or Equals. Neither of these fits with what you are trying to do and will only cause confusion, as evidenced by the answers written here. Write your own method called something like IntersectsWith. Have a look into any decent geometry library (e.g. boost if you're happy pulling from c++) as how to go about doing this. As to casting from bool to int, this can be easily done by using the bool with the ? ternary operator.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top