1

Can someone explain to me how this is supposed to work? I followed an MSDN example I found at http://msdn.microsoft.com/en-us/library/234b841s.aspx .

I have made my own CustomObject and have made a Comparer for it.

Here is the CustomObjectComparer class:

public class CustomObjectComparer : System.Collections.Generic.IComparer<CustomObject>
{
    public int Compare(CustomObject co1, CustomObject co2)
    {
         //Impementation Omitted
    }

}

Then when I have a List<CustomObject> and try to do the following I get compile errors.

List<CustomObject> list = new List<CustomObject>();
CustomObjectComparer comparer = new CustomObjectComparer();
list.Sort(comparer);

Errors: Argument 1: cannot convert from 'CustomObjectComparer' to 'System.Collections.Generic.IComparer<CustomObject>'

Isn't CustomObjectComparer a System.Collections.Generic.IComparer?

Atari2600
  • 2,643
  • 2
  • 20
  • 18

1 Answers1

4

It looks your list contains CustomObjectComparers, not CustomObjects.

You can either pass a comparer that can compare CustomObjectComparers, or (more likely) change the list to a List<CustomObject>.

EDIT: This would happend if you have two types named CustomObject, or if you also have a different error.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I agree with SLaks. Show us the code where you initialize the "list" variable. I believe that may be your problem – randbrown Mar 03 '11 at 16:50
  • Thanks guys, I copied over the error wrong. I have editted to be correct and also have added the initialization of the "list" variable. – Atari2600 Mar 03 '11 at 17:04
  • Hey SLaks, that comment helped me. I am trying to create a SortableBindingList for CustomObject and had declared it like `code` public class CustomObjectSortableBindingList : BindingList`code` When I removed that and tried, it compiled just fine. Looks like a I need a different approach. Thanks for your help. – Atari2600 Mar 03 '11 at 17:11