Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot convert to IComparer"

Tags:

c#

icomparer

I have the following IComparer defined for boxed RegistryItem objects:

public class BoxedRegistryItemComparer : IComparer<object>
{
    public int Compare(object left, object right)
    {
        RegistryItem leftReg = (RegistryItem)left;
        RegistryItem rightReg = (RegistryItem)right;

        return string.Compare(leftReg.Name, rightReg.Name);
    }
}

I want to use this to sort an ArrayList of boxed RegistryItems (It really should be a List<RegistryItem>, but that's out of my control).

ArrayList regItems = new ArrayList();
// fill up the list ...
BoxedRegistryItemComparer comparer = new BoxedRegistryItemComparer();
ArrayList.sort(comparer);

However, the last line gives the compiler error: "Cannot convert from BoxedRegistryItemComparer to System.Collections.IComparer". I would appreciate it if someone could point out my mistake.

like image 370
Odrade Avatar asked Sep 28 '09 13:09

Odrade


Video Answer


1 Answers

BoxedRegistryItemComparer should implement System.Collections.IComparer to be used with ArrayList.Sort. you implemented System.Collections.Generic.IComparer<T> which is not the same thing.

like image 199
manji Avatar answered Sep 28 '22 14:09

manji