CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2008
    Posts
    154

    Fix Code in code example???

    I am trying to use this guy's code but I don't know enough on how to fix it - because he was a little lazy in typing it.

    from here>
    http://blog.codewrench.net/2009/04/1...rary-property/

    code that needs fixing
    Code:
    public enum SortDirection
    {
       Ascending, Descending
    }
    
    public class ListSorter where T : class
    {
      public static List Sort
    (
                  List listToSort,
                  string propertyName,
                  SortDirection direction) where P : IComparable
      {
        Type propertyType = typeof (P);
        Type comparableInterface = propertyType.GetInterface("IComparable");
    
        if (comparableInterface == null)
            throw new Exception("Properties to sort by must be IComparable");
    
        listToSort.Sort(
            delegate(T x, T y)
                {
                  PropertyInfo p1 = x.GetType().GetProperty(propertyName, propertyType);
                  PropertyInfo p2 = y.GetType().GetProperty(propertyName, propertyType);
    
                  object p1objvalue = p1.GetValue(x, null);
                  object p2objvalue = p2.GetValue(y, null);
    
                  P p1value = (P)p1objvalue;
                  P p2value = (P)p2objvalue;
    
                  if (direction == SortDirection.Ascending)
                      return p1value.CompareTo(p2value);
                  else
                      return p2value.CompareTo(p1value);
              });
    
        return listToSort;
      }
    }
    I was able to fix a small part of it, namely here >>

    Code:
    public class ListSorter<T> where T : class
    but I can't solve the rest
    Last edited by bixel; July 2nd, 2011 at 02:41 PM.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Fix Code in code example???

    Fix what exactly? what doesn't work? You should expect us to do the legwork for you, narrow the problem description down.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured