473,769 Members | 5,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ICompare class for a list of struct with generic fields

Hello,

I have a list of struct's that have generic fields:

public struct Item<T1, T2: IComparable<Ite m<T1, T2>{
private T1 _t1;
private T2 _t2;

public T1 t1 {
get { return _t1; }
}

public T2 t2 {
get { return _priority; }
}

internal Item(T1 at1, T2 at2) {
this._t1 = at1;
this._t2 = at2;
}

public int CompareTo(Item< T1, T2other) {
return Comparer<T2>.De fault.Compare(t his.t2, other.t2);
}
}

My question is how to build an IComparer class. Specifically, in the
following, what must X be so that the compiler will be OK with Y & Z.
Have I run into some language syntax limitation? Maybe an interface is
required?

public class MyCompare<X : IComparer<X>
where X : IComparable<Ite m<Y,Z>{

public Compare(Item<Y, Zitem1, Item<Y,Zitem2) { ... }

public bool Equals(Item<Y, Zitem1, Item<Y, Zitem2) { ... }

public int GetHashCode(Ite m<Y, Zitem) {
}

public class MyList<T1, T2: IEnumerable<Ite m<T1, T2>>, ICloneable {

protected List<Item<T1, T2>myList = new Item<T1, T2>>();

protected IComparer<Item< T1, T2>myComparer = new MyCompare<Item< T1,
T2>>();

...

}
Thanks,

Bill

(This is a repost (under my MSDN account) of an early post; hopefully
this is more clear.)
Sep 26 '08 #1
4 2076
On Sep 26, 3:21*pm, Bill McCormick <wpmccorm...@ne wsgroup.nospam>
wrote:

<snip>
My question is how to build an IComparer class. Specifically, in the
following, what must X be so that the compiler will be OK with Y & Z.
Have I run into some language syntax limitation? Maybe an interface is
required?

public class MyCompare<X*: IComparer<X>
* * *where X : IComparable<Ite m<Y,Z>{

* *public Compare(Item<Y, Zitem1, Item<Y,Zitem2) { ... }

* *public bool Equals(Item<Y, Zitem1, Item<Y, Zitem2) { ... }

* *public int GetHashCode(Ite m<Y, Zitem) {

}
Why do you think you need an X at all? It seems to me that you need Y
and Z as type parameters, but not X:

public class MyComparer<Y, Z: IComparer<Item< Y, Z>>

It's not really clear what you're trying to accomplish though...

Jon
Sep 26 '08 #2
Jon Skeet [C# MVP] wrote:
On Sep 26, 3:21 pm, Bill McCormick <wpmccorm...@ne wsgroup.nospam>
wrote:

<snip>
>My question is how to build an IComparer class. Specifically, in the
following, what must X be so that the compiler will be OK with Y & Z.
Have I run into some language syntax limitation? Maybe an interface is
required?

public class MyCompare<X : IComparer<X>
where X : IComparable<Ite m<Y,Z>{

public Compare(Item<Y, Zitem1, Item<Y,Zitem2) { ... }

public bool Equals(Item<Y, Zitem1, Item<Y, Zitem2) { ... }

public int GetHashCode(Ite m<Y, Zitem) {

}

Why do you think you need an X at all? It seems to me that you need Y
and Z as type parameters, but not X:

public class MyComparer<Y, Z: IComparer<Item< Y, Z>>

It's not really clear what you're trying to accomplish though...

Jon
I'm trying to make a custom comparer for sorting a generic list that
contains struct objects with generic fields. X is more of variable for
my question than a generic. I agree, Y & Z are needed, but how? The
following code ...

public class MyCompare<Y,Z : IComparer<Item< Y,Z>{ ... }
results in the compiler complaining ...

Error 1 Using the generic type 'MyCompare<Y,Z> ' requires '2'
type arguments ...

.... when it tries to compile this:

protected IComparer<Item< T1, T2>myComparer = new MyCompare<Item< T1,
T2>>();

Sep 26 '08 #3
On Sep 26, 3:59*pm, Bill McCormick <wpmccorm...@ne wsgroup.nospam>
wrote:
I'm trying to make a custom comparer for sorting a generic list that
contains struct objects with generic fields. X is more of variable for
my question than a generic. I agree, Y & Z are needed, but how? The
following code ...

public class MyCompare<Y,Z*: IComparer<Item< Y,Z>{ ... }

results in the compiler complaining ...

Error * 1 * * * Using the generic type 'MyCompare<Y,Z> ' requires '2'
type arguments *...

... when it tries to compile this:

protected IComparer<Item< T1, T2>myComparer = new MyCompare<Item< T1,
T2>>();
Yes - you're trying to create an instance just passing in Item<T1, T2>
when you need to pass in T1 and T2 as type arguments:

protected IComparer<Item< T1, T2>myComparer = new MyCompare<T1,
T2>();

Jon
Sep 26 '08 #4
Jon Skeet [C# MVP] wrote:
On Sep 26, 3:59 pm, Bill McCormick <wpmccorm...@ne wsgroup.nospam>
wrote:
>I'm trying to make a custom comparer for sorting a generic list that
contains struct objects with generic fields. X is more of variable for
my question than a generic. I agree, Y & Z are needed, but how? The
following code ...

public class MyCompare<Y,Z : IComparer<Item< Y,Z>{ ... }

results in the compiler complaining ...

Error 1 Using the generic type 'MyCompare<Y,Z> ' requires '2'
type arguments ...

... when it tries to compile this:

protected IComparer<Item< T1, T2>myComparer = new MyCompare<Item< T1,
T2>>();

Yes - you're trying to create an instance just passing in Item<T1, T2>
when you need to pass in T1 and T2 as type arguments:

protected IComparer<Item< T1, T2>myComparer = new MyCompare<T1,
T2>();

Jon
Thanks Jon. That did the trick. It was confusing trying to follow the
pattern ...

public class MyCompare<T : IComparer<T{ ... }

public class MyList<T: IEnumerable<T>, ICloneable {

protected List<TmyList = new List<T>();

protected IComparer<TmyCo mparer = new MyCompare<T>();

...

}

.... and trying to substitute Item<T1, T2for <T>.
Bill
Sep 26 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2038
by: chirag | last post by:
hi i am writing the following function . but does not seem to put the code in sequence. it does not compile. its kind of messed up. please help me improve it. thanks. void addToEndOfLinkedList(Node * &head) // Reads a series of integers from the keyboard (ending with -1) and // appends them to the end of the linked list pointed to by head. No // error checking is done. Correctly handles the case when the // original list starts out...
12
2514
by: Jonathan Bartlett | last post by:
Just finished a new IBM DeveloperWorks article on linked lists, and thought you all might be interested. It's not an introduction -- it instead covers some of the more interesting aspects of linked lists. http://www.ibm.com/developerworks/linux/library/l-listproc/ Jon ---- Learn to program using Linux assembly language http://www.cafeshops.com/bartlettpublish.8640017
12
3601
by: Sadeq | last post by:
Is there a way to read a struct field by field? I want to write a rather general function, like: public string EncodeStruct (struct strct) { .... } which accepts a general struct type, reads it field by field, encodes
17
2237
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have an algorithm that modifies this datastructure. The basic outline of the algorithm is independent of the type of property. So I implemented a generic version of the algorithm and a function object for
37
4025
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested that I implement it as a struct rather than a class which I did and all worked OK. The simplest declaration for the struct is:
1
2763
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define the fixed size using In the attached console application example when I try to address the fields in the class I get an execption.
2
1896
by: Dom Jackson | last post by:
Hello - I have a problem where I need to test some numeric code using a variety of built-in integer types: obj_type1 = obj_type2 OP obj_type3; // is obj_type1 correct? If I test with 10 built-in integer types, then I get 1000 permutations of the above statement. If I then test a dozen different operators, I get over 10,000 test operations.
3
4388
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;
6
391
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
10222
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
10050
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...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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
5310
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...
1
3967
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
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.