473,372 Members | 1,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 4372
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Frank Wisniewski | last post by:
Hi All, I have created my own generic comparer object which implements IComparer and uses reflection. I have tested it and it seem to work fine but I am worried about the way I did it. My goal...
5
by: Narshe | last post by:
I have a custom collection class that looks like this public class CustomCollection<T> : ICollection<T>, IList<T>, IEnumerable<T>, IDisposable, ICloneable, IComparer<T> {....} I need to know...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
3
by: =?Utf-8?B?R0I=?= | last post by:
I have created a small program that illustrates the problem. I would know how to address the fields that I want to sort on in the greaterThan comparison. Anybody who knows?? using System; ...
2
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a generic list populated by a structure and sorts the contents of the list based on the day and project # that is part of the structure. I would like to modify it to also sort on the...
0
by: Buky | last post by:
So, Let's say that I have an class with properies like Name, LastName & ID. I put them into List(of myClass) and I would like to search through that List based on my search criteria (Name & Id for...
0
by: wpmccormick | last post by:
I'm trying to make a custom Comparer for a priority queue and I am stumped on the syntax for the Generics. I think I almost have it (below) except for where the ??'s are. Anybody done this before?...
4
by: Bill McCormick | last post by:
Hello, I have a list of struct's that have generic fields: public struct Item<T1, T2: IComparable<Item<T1, T2>{ private T1 _t1; private T2 _t2; public T1 t1 { get { return _t1; }
5
by: JC | last post by:
Hi all, I have scoured the internet for articles on sorting multi-dimensional arrays of generic types e.g. T using the IComparer<Tinterface and have run into a real roadblack. There does not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.