Question about equality

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Burland

    #1

    Question about equality

    I'm a little confused about the way the default equality operator works with
    classes. Here's the situation, I have two comboboxes that are each filled
    with different object (i.e. ComboBox1 contains objects of class A, ComboBox2
    contains objects of class B). What I'm trying to do is determine if a given
    object is contained in one of the comboboxes, i.e.:

    Combobox1.Items .Contains(MyA);
    Combobox2.Items .Contains(MyB);

    Now the problem is that this works fine for class A, but not for class B. In
    class B two clearly identical (to me) objects are failing to return true
    when I test for equality, i.e:

    MyA == IdenticalCopyOf MyA; // true
    MyB == IdenticalCopyOf MyB; // false

    This is despite the fact that all the fields appear to be identical. So what
    is actually being compared in the default Equals method and the equality
    operator. I thought it performed a simple comparison of each field unless it
    was overridden, but that doesn't seem to be the case here.
    I can fix my particular problem here by overriding Equals and == in class B,
    but I'd like to understand why this is happening.

    Cheers

    Matt


  • Joe Mayo

    #2
    Re: Question about equality



    "Matt Burland" <anonymous@disc ussions.microso ft.com> wrote in message
    news:%23tFMYyVq DHA.1948@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > I'm a little confused about the way the default equality operator works[/color]
    with[color=blue]
    > classes. Here's the situation, I have two comboboxes that are each filled
    > with different object (i.e. ComboBox1 contains objects of class A,[/color]
    ComboBox2[color=blue]
    > contains objects of class B). What I'm trying to do is determine if a[/color]
    given[color=blue]
    > object is contained in one of the comboboxes, i.e.:
    >
    > Combobox1.Items .Contains(MyA);
    > Combobox2.Items .Contains(MyB);
    >
    > Now the problem is that this works fine for class A, but not for class B.[/color]
    In[color=blue]
    > class B two clearly identical (to me) objects are failing to return true
    > when I test for equality, i.e:
    >
    > MyA == IdenticalCopyOf MyA; // true
    > MyB == IdenticalCopyOf MyB; // false
    >
    > This is despite the fact that all the fields appear to be identical. So[/color]
    what[color=blue]
    > is actually being compared in the default Equals method and the equality
    > operator. I thought it performed a simple comparison of each field unless[/color]
    it[color=blue]
    > was overridden, but that doesn't seem to be the case here.
    > I can fix my particular problem here by overriding Equals and == in class[/color]
    B,[color=blue]
    > but I'd like to understand why this is happening.[/color]

    Hi Matt,

    I would have to see the object implementations to tell for sure why the
    difference between type A and type B. If a object is a value type, you will
    have value equality. If the object is a reference type, the default
    behavior is reference equality. To get value equality for a reference type,
    you will have to implement Equals in your type (and == while your at it).

    Also, look at how you are creating IdenticalCopyOf MyA and
    IdenticalCopyOf MyB. Are both objects IClonable?Are the implementations
    different? Is it possible that you are returning a reference to A, but
    creating a new instance of B?

    Joe
    --
    Learn C# with step-by-step tutorials and practical examples. Start your first program, explore lists and parsing, and understand how .NET runs your code.



    Comment

    • Madhu [MVP]

      #3
      Question about equality

      Hi,

      Both the equality and the == operator works the same way
      for classes. It only does reference equality rather than
      value equality. To do value equality you need to override
      equals.

      Hope this helps...

      Regards,
      Madhu

      MVP | MCSD.NET
      [color=blue]
      >-----Original Message-----
      >I'm a little confused about the way the default equality[/color]
      operator works with[color=blue]
      >classes. Here's the situation, I have two comboboxes[/color]
      that are each filled[color=blue]
      >with different object (i.e. ComboBox1 contains objects[/color]
      of class A, ComboBox2[color=blue]
      >contains objects of class B). What I'm trying to do is[/color]
      determine if a given[color=blue]
      >object is contained in one of the comboboxes, i.e.:
      >
      >Combobox1.Item s.Contains(MyA) ;
      >Combobox2.Item s.Contains(MyB) ;
      >
      >Now the problem is that this works fine for class A, but[/color]
      not for class B. In[color=blue]
      >class B two clearly identical (to me) objects are[/color]
      failing to return true[color=blue]
      >when I test for equality, i.e:
      >
      >MyA == IdenticalCopyOf MyA; // true
      >MyB == IdenticalCopyOf MyB; // false
      >
      >This is despite the fact that all the fields appear to[/color]
      be identical. So what[color=blue]
      >is actually being compared in the default Equals method[/color]
      and the equality[color=blue]
      >operator. I thought it performed a simple comparison of[/color]
      each field unless it[color=blue]
      >was overridden, but that doesn't seem to be the case[/color]
      here.[color=blue]
      >I can fix my particular problem here by overriding[/color]
      Equals and == in class B,[color=blue]
      >but I'd like to understand why this is happening.
      >
      >Cheers
      >
      >Matt
      >
      >
      >.
      >[/color]

      Comment

      • 100

        #4
        Re: Question about equality

        Hi Matt,

        Is the type of MyA a *struct* insted of *class*? The default implementation
        for classes (reference types) is to compare the references (addresses) only.
        For value types (struct and enum) the default implementation uses reflection
        to compare the fields of the objects.

        So, if you have two identical copies of an object (two different objects
        with identical fields) obj1 and obj2
        obj1 == obj2 will return:
        *false* if the objects are instances of reference type (class)
        *true* if the objects are instances of value type (struct or enum)

        HTH
        B\rgds
        100

        "Matt Burland" <anonymous@disc ussions.microso ft.com> wrote in message
        news:%23tFMYyVq DHA.1948@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        > I'm a little confused about the way the default equality operator works[/color]
        with[color=blue]
        > classes. Here's the situation, I have two comboboxes that are each filled
        > with different object (i.e. ComboBox1 contains objects of class A,[/color]
        ComboBox2[color=blue]
        > contains objects of class B). What I'm trying to do is determine if a[/color]
        given[color=blue]
        > object is contained in one of the comboboxes, i.e.:
        >
        > Combobox1.Items .Contains(MyA);
        > Combobox2.Items .Contains(MyB);
        >
        > Now the problem is that this works fine for class A, but not for class B.[/color]
        In[color=blue]
        > class B two clearly identical (to me) objects are failing to return true
        > when I test for equality, i.e:
        >
        > MyA == IdenticalCopyOf MyA; // true
        > MyB == IdenticalCopyOf MyB; // false
        >
        > This is despite the fact that all the fields appear to be identical. So[/color]
        what[color=blue]
        > is actually being compared in the default Equals method and the equality
        > operator. I thought it performed a simple comparison of each field unless[/color]
        it[color=blue]
        > was overridden, but that doesn't seem to be the case here.
        > I can fix my particular problem here by overriding Equals and == in class[/color]
        B,[color=blue]
        > but I'd like to understand why this is happening.
        >
        > Cheers
        >
        > Matt
        >
        >[/color]


        Comment

        • Matt Burland

          #5
          Re: Question about equality

          > Also, look at how you are creating IdenticalCopyOf MyA and[color=blue]
          > IdenticalCopyOf MyB. Are both objects IClonable?Are the implementations
          > different? Is it possible that you are returning a reference to A, but
          > creating a new instance of B?
          >[/color]

          Thanks for the reply, now that I look through the whole thing again I think
          my copy of A was in fact a reference to A and not a separate A, while in B I
          was looking at a separate object of class B. Makes sense now.


          Comment

          Working...