471,348 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Generic.IComparer question

I am trying to use the following generic (reflection) class as the
ICamparer parameter for a generic list..but I get the error:
"Unable to cast object of type 'GenericComparer' to type
'System.Collections.Generic.IComparer `1[AccountDB.Queue]'

My code looks like the following:

List<AccountDB.Queue> oList = getAllQueuesFunction();
oList.Sort(New GenericComparer("QueueName"));

This is my GenericComparer:

public class GenericComparer : IComparer
{
string propertyName;

public GenericComparer(string propertyName)
{
this.propertyName = propertyName;
}

public int Compare(object x, object y)
{
// gets the value of the x property
PropertyInfo property =
x.GetType().GetProperty(propertyName);
object valueOfX = property.GetValue(x, null);

// gets the value of the y property
property = y.GetType().GetProperty(propertyName);
object valueOfY = property.GetValue(y, null);

// now makes the comparsion
return ((IComparable)valueOfX).CompareTo(valueOfY);
}
}

Hopefull you can see where I'm trying to go with this. Any
suggestions? Am I approaching this the wrong way?

Jan 7 '06 #1
1 4280
INeedADip wrote:
I am trying to use the following generic (reflection) class as the
ICamparer parameter for a generic list..but I get the error:
"Unable to cast object of type 'GenericComparer' to type
'System.Collections.Generic.IComparer `1[AccountDB.Queue]'

My code looks like the following:

List<AccountDB.Queue> oList = getAllQueuesFunction();
oList.Sort(New GenericComparer("QueueName"));

This is my GenericComparer:

public class GenericComparer : IComparer

Try defining it as:

public class GenericComparer<T> : IComparer<T>, IComparer
(+ some changes to the method definition, etc., also note that I listed
both the generic version and the non-generic version so that it can be
used with non-generic collections as well)

Since you do the manual work inside you're not actually gaining anything
by defining it like this, but it would be compatible with the
requirements of the Sort method, which would then be called like this:

oList.Sort(new GenericComparer<AccountDB.Queue>("QueueName"));

Note that this is off the top of my head so test it and yell at me if I
got it wrong :)

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Jan 9 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Frank Wisniewski | last post: by
5 posts views Thread by Narshe | last post: by
4 posts views Thread by Adam Clauss | last post: by
3 posts views Thread by =?Utf-8?B?R0I=?= | last post: by
2 posts views Thread by =?Utf-8?B?UGF1bA==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.