Advertisement

02.07.2008 at 04:31PM PST, ID: 23146543
[x]
Attachment Details

How to use generic type T with delegate?

Asked by Valimai in C# Programming Language

hi there,

I am in the middle of refactoring my class to be a generic class.  I am having problems with converting the CompareTo part of the code.

Fact: T has a Int32 property called MyIndex

Here is the Half converted code. CompareTo is giving me grief :)
<snip>
    List<T> myList = new List<T>();
    myList.Sort(delegate(T a, T b)
    {
        return a.MyIndex.CompareTo(b.MyIndex);
    });
</snip>

return a.MyIndex.CompareTo(b.MyIndex); // <--- Help with this part of the conversion pls.

I have tried something like but doesn't work.
return a.GetType().GetProperty("MyIndex").CompareTo(b.GetType().GetProperty("MyIndex"));

thanksStart Free Trial
 
Loading Advertisement...
 
[+][-]02.07.2008 at 04:45PM PST, ID: 20846919

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C# Programming Language
Sign Up Now!
Solution Provided By: jaime_olivares
Participating Experts: 2
Solution Grade: A
 
 
[+][-]02.07.2008 at 06:01PM PST, ID: 20847254

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.07.2008 at 10:18PM PST, ID: 20848216

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628