473,398 Members | 2,389 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,398 software developers and data experts.

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<Item<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>.Default.Compare(this.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<Item<Y,Z>{

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

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

public int GetHashCode(Item<Y, Zitem) {
}

public class MyList<T1, T2: IEnumerable<Item<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 2062
On Sep 26, 3:21*pm, Bill McCormick <wpmccorm...@newsgroup.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<Item<Y,Z>{

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

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

* *public int GetHashCode(Item<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...@newsgroup.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<Item<Y,Z>{

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

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

public int GetHashCode(Item<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...@newsgroup.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...@newsgroup.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<TmyComparer = 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
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...
12
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...
12
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,...
17
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...
37
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...
1
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...
2
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...
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; ...
6
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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.