I've implemented an interface which all objects that need ordered will implement.
I'm still having problems. Here is a break down of my code: I have an IOrderedChild interface: public interface IOrderedChild { int Index { get; set; } } A Question Element that implements IOrderedChild public class QuestionElement : DBObjectBase, IOrderedChild { public int Index { get { return _index; } set { _index = value; } } } I have a Set class that I reads as follows: public class Set<T, U> : IEnumerable where T : IEnumerable, ICollection, ISet where U : DBObjectBase, IOrderedChild { private List<U> _items; public Set(T list) { _items = new List<U>(); IEnumerator listEnumerator = list.GetEnumerator(); while (listEnumerator.MoveNext()) _items.Add((U)listEnumerator.Current); } public Set(T list, bool sort): this(list) { ELementComparer comparer = new ELementComparer(); _items.Sort(comparer); } } The comparer looks like this: public class ELementComparer : IComparer<IOrderedChild> { int IComparer<IOrderedChild>.Compare(IOrderedChild x, IOrderedChild y) { return (x.Index > y.Index) ? 1 : (x.Index < y.Index) ? -1 : 0; } } This will simply not compile. I get the cannot convert error. I'm going mad. I really wanted to derive a new class from Set that would have the extra constraint on U and put the overloaded constructor in the derived class. Thanks for helping Paul [EMAIL PROTECTED]
From: Ryan Heath <[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 15:13:10 +0100 Hi Paul Cowan, you could also implement an IComparable<> on your QuestionElement class class QuestionElement : IComparable<QuestionElement> { ... int IComparable<QuestionElement>.CompareTo(QuestionElement other) { return Comparer<int>.Default.Compare(this.Index, other.Index); } } by then you do not need to specify a Comparer for the items.Sort function BTW there is a design flaw in your case: by using ElementComparer you are saying more or less that U *is* a QuestionElement (or descendant of) ... So why is U a generic parameter? HTH // Ryan On 1/25/06, Paul Cowan <[EMAIL PROTECTED]> wrote: > 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