Question

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.

Était-ce utile?

La solution

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>

Autres conseils

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)".

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top