-1

okay, i followed many tutorial to make it work but i never found a solution.

i have this function in a static class:

        public static bool isDifferent<T>(List<T> list1, List<T> list2) where T : IComparable
    {
        foreach (T item1 in list1)
        {
            bool different = false;
            foreach (T item2 in list2)
            {
                if (item2.CompareTo(item1) != 0)
                    different = true;
                else
                {
                    different = false;
                    break;//fuck yes i will use a break
                }
            }
            if (different)
                return true;
        }
        return false;
    }

it work well with list of int but now i want to compare a list of a custom class named room.

my class declaration have all he need : public class Room : IComparable

and i added the CompareTo function.

        public int CompareTo(Room other)
    {
        if (other == null) return 1;

        if (Methods.isDifferent(doors, other.doors))
            return 1;
        else 
            return 0;

    }

so, each room have a list of hallway id and this the only value i need to compare.

i followed many tutorial and they seems to have the same structure as mine.

2 Answers2

1

From you question, i can deduce that you implement the interface IComparable<Room>.

However, this interface IComparable<T> is not assignable to IComparable.

Implement IComparable, which declares the method "public int CompareTo(object other)".

  • public class Room : IComparable { public List doors; public int CompareTo(Room other) { if (other == null) return 1; if (Methods.isDifferent(doors, other.doors)) return 1; else return 0; } } – Cédric Raymond May 15 '14 at 19:52
  • @CédricRaymond, as i said, i deduced this already (and so did Ben in his answer, too...) –  May 15 '14 at 19:53
  • sorry i did not notice the your answer was also good but his was clearer =p – Cédric Raymond May 15 '14 at 19:57
  • i also had to edit my question for the same reason. And i dont understand why someone downvoted my question. It was a legit question – Cédric Raymond May 15 '14 at 20:00
  • I have no idea about the downvote too. It is common to confuse IComparer and IComparer (even in code review or discussion amongst peers, where you would just say something like "this type implements IComparer" and everybody starts asking "Which one, the generic or non-generic?" ;) ) –  May 15 '14 at 20:03
1

Your problem is that Room implements IComparable<Room>, which is not the same interface as IComparable. You should probably update your method to:

public static bool isDifferent<T>(List<T> list1, List<T> list2) where T : IComparable<T>
Ben Aaronson
  • 6,955
  • 2
  • 23
  • 38