473,760 Members | 8,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic.ICompar er 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 'GenericCompare r' to type
'System.Collect ions.Generic.IC omparer `1[AccountDB.Queue]'

My code looks like the following:

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

This is my GenericComparer :

public class GenericComparer : IComparer
{
string propertyName;

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

public int Compare(object x, object y)
{
// gets the value of the x property
PropertyInfo property =
x.GetType().Get Property(proper tyName);
object valueOfX = property.GetVal ue(x, null);

// gets the value of the y property
property = y.GetType().Get Property(proper tyName);
object valueOfY = property.GetVal ue(y, null);

// now makes the comparsion
return ((IComparable)v alueOfX).Compar eTo(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 4397
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 'GenericCompare r' to type
'System.Collect ions.Generic.IC omparer `1[AccountDB.Queue]'

My code looks like the following:

List<AccountDB. Queue> oList = getAllQueuesFun ction();
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.Queu e>("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***@vk arlsen.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
1832
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 was to not have to make a seperate Comparer class for every field of my object that I wanted to sort like the ASP.net TimeTracker starter kit did. My Code is below. Basically to use it all I have to do is to Create the GenericComparer object,...
5
8931
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 if the type T has implemented a certain interface ICustomClass. I can't do
4
2223
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 the old code allowed me to do what basically was the following assignment: class SomeClass { private Queue q; SomeClass(Queue q)
3
4387
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; class Program { public List<itemitList, index1;
2
4475
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 start time as the third sort parameter? thanks. public class Comparer : IComparer<mergeoutstruct> { public int Compare(mergeoutstruct a, mergeoutstruct b) {
0
1262
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 example). So, I create an Comparer, and provide it's 'reference' to an BinarySearch functio, but each time I got incorrect results. Where could be problem in following code? Public Class myClass Public Id As Int32 Public Name As String
0
918
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? Thanks, Bill struct Item<TValue, TPriority{ }
4
2076
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
5263
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 seem to be an easy way to do this. I have found implementations that use the non-generic IComparer interface but it seems to me to be more elegant to use generics if possible. Has anyone else had this problem and could they suggest or poin me
0
10107
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9945
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.