ICompare class for a list of struct with generic fields

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill McCormick

    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.)
  • Jon Skeet [C# MVP]

    #2
    Re: ICompare class for a list of struct with generic fields

    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

    Comment

    • Bill McCormick

      #3
      Re: ICompare class for a list of struct with generic fields

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

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: ICompare class for a list of struct with generic fields

        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

        Comment

        • Bill McCormick

          #5
          Re: ICompare class for a list of struct with generic fields

          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

          Comment

          Working...