Generics and comparison (follow on from my previous "Generics and collections" thread)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Robins

    Generics and comparison (follow on from my previous "Generics and collections" thread)

    Marc has sorted my problem with the collections, however I am now in a new scenario; I am trying to compare a generic property value before setting it as shown below ...

    public T Value {
    get { return this.value; }
    set {
    if ( this.value != value ) {
    this.value = value;
    this.OnValueCha nged(EventArgs. Empty);
    }
    }
    }

    This code produces the Operator '==' cannot be applied to operands of type 'T' and 'T' error at compile time!
    I am sure that I am not the only person to have tried this, any clues as to the correct work around?

    I have tried restricting 'T' to IComparable in a where clause and then casting 'this.value' and 'value' to IComparable for the comparison - the code then compiles but is this the right answer? It feels wrong, but I am quickly learning that my own understanding of generics is much lower than I thought it was!

    Thanks in advance.

  • Martin Robins

    #2
    Re: Generics and comparison (follow on from my previous "Generi cs and collections&quo t; thread)

    Though it compiles, the original IComparable idea did not work; the comparisons would often return incorrect results.

    I have resolved it for now using "if ( this.value == null ? value != null : !this.value.Equ als(value) )" but I am still open to a better way if it exists ...

    Martin.

    "Martin Robins" <martin at orpheus-solutions dot co dot ukwrote in message news:ujRpfRnlIH A.536@TK2MSFTNG P06.phx.gbl...
    Marc has sorted my problem with the collections, however I am now in a new scenario; I am trying to compare a generic property value before setting it as shown below ...

    public T Value {
    get { return this.value; }
    set {
    if ( this.value != value ) {
    this.value = value;
    this.OnValueCha nged(EventArgs. Empty);
    }
    }
    }

    This code produces the Operator '==' cannot be applied to operands of type 'T' and 'T' error at compile time!
    I am sure that I am not the only person to have tried this, any clues as to the correct work around?

    I have tried restricting 'T' to IComparable in a where clause and then casting 'this.value' and 'value' to IComparable for the comparison - the code then compiles but is this the right answer? It feels wrong, but I am quickly learning that my own understanding of generics is much lower than I thought it was!

    Thanks in advance.

    Comment

    • Rudy Velthuis

      #3
      Re: Generics and comparison (follow on from my previous &quot;Generi cs and collections&quo t; thread)

      Martin Robins wrote:
      Though it compiles, the original IComparable idea did not work; the
      comparisons would often return incorrect results.
      That is a problem with the implementation of the interface for that
      particular instantiation of T, then. If it were correctly implemented,
      that should not happen.
      --
      Rudy Velthuis http://rvelthuis.de

      "Sailors ought never to go to church. They ought to go to hell,
      where it is much more comfortable." -- HG Wells.

      Comment

      Working...