tags:

views:

229

answers:

3

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.

+6  A: 

You've defined a Generic-Comparer (IComparer(Of T)) instead of a Comparer without a type (IComparer). Change the declaration and it should work.

Bobby
+3  A: 

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.

najmeddine
A: 

public class BoxedRegistryItemComparer : IComparer { ... }

MaLio