ICompare class for a list of struct with generic fields


B

Bill McCormick

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, T2> other) {
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,Z> item1, Item<Y,Z> item2) { ... }

public bool Equals(Item<Y, Z> item1, Item<Y, Z> item2) { ... }

public int GetHashCode(Item<Y, Z> item) {
}

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.)
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

On Sep 26, 3:21 pm, Bill McCormick <[email protected]>
wrote:

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,Z> item1, Item<Y,Z> item2) { ... }

   public bool Equals(Item<Y, Z> item1, Item<Y, Z> item2) { ... }

   public int GetHashCode(Item<Y, Z> item) {

}

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
 
B

Bill McCormick

Jon said:
On Sep 26, 3:21 pm, Bill McCormick <[email protected]>
wrote:



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>>();
 
J

Jon Skeet [C# MVP]

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
 
Ad

Advertisements

B

Bill McCormick

Jon said:
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<T> myList = new List<T>();

protected IComparer<T> myComparer = new MyCompare<T>();

...

}

.... and trying to substitute Item<T1, T2> for <T>.


Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top