Sorry Paul, You are correct as you can tell I'm quite new to this.
Thanks [EMAIL PROTECTED]
From: Paul van Brenk <[EMAIL PROTECTED]> Reply-To: "Discussion of advanced .NET topics." <ADVANCED-DOTNET@DISCUSS.DEVELOP.COM> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM Subject: Re: [ADVANCED-DOTNET] Generics Comiler error/headache Date: Wed, 25 Jan 2006 14:37:39 +0100 The ElementComparer will only work on collections of type QuestionElement, since that is what you specified when you defined the type. So unless the compiler can make sure U will be always be of type QuestionElement, then it will not compile. Paul -----Original Message----- From: Discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf Of Paul Cowan Sent: Wednesday, January 25, 2006 14:32 To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM Subject: Re: [ADVANCED-DOTNET] Generics Comiler error/headache U is a parameterized type I am passing into the class. It still will not compile. Thanks [EMAIL PROTECTED] >From: Paul van Brenk <[EMAIL PROTECTED]> >Reply-To: "Discussion of advanced .NET topics." ><ADVANCED-DOTNET@DISCUSS.DEVELOP.COM> >To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM >Subject: Re: [ADVANCED-DOTNET] Generics Comiler error/headache >Date: Wed, 25 Jan 2006 13:57:23 +0100 > >Your ElementComparar is defined for the specific type QuestionElement, >so downcasting it to the more generic type won't work. > >This code should compile: > >List<QuestionElement> _items; > >public Set(T list) >{ >_items = new List<QuestionElement>(); >//code to load collection >ElementComparer comparer = new ElementComparer(); >_items.Sort(comparer); //WON'T COMPILE >} > > > >-----Original Message----- >From: Discussion of advanced .NET topics. >[mailto:[EMAIL PROTECTED] On Behalf Of Paul Cowan >Sent: Wednesday, January 25, 2006 13:49 >To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM >Subject: [ADVANCED-DOTNET] Generics Comiler error/headache > >I am trying to do a custom sort on using the following comparer: > >public class ElementComparer : IComparer<QuestionElement> >{ >public int Compare(QuestionElement x, QuestionElement y) >{ >return (x.Index > y.Index) ? 1 : (x.Index < y.Index) ? -1 : >0; >} >} > >I have the following code in the constructor of another object: > >Then in the constructor of another class, I am trying to sort on a >List<U> like this: > >public Set(T list) >{ >_items = new List<U>(); >//code to load collection >ElementComparer comparer = new ElementComparer(); >_items.Sort(comparer); //WON'T COMPILE >} > >The compiler error is cannot convert ElementComparer to >IComparer<U>. What am I doing wrong. > >Thanks > >Paul > >=================================== >This list is hosted by DevelopMentor(r) http://www.develop.com > >View archives and manage your subscription(s) at >http://discuss.develop.com > >=================================== >This list is hosted by DevelopMentor(r) http://www.develop.com > >View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor(r) http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
=================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com