Overriding == and testing to null

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • EL OSO

    #1

    Overriding == and testing to null

    Hi, let's suppose I have a

    class A
    {
    int x;
    ...
    public static bool operator == (A a1 , A a2)
    {
    return (a1.x==a2.x);
    }
    }

    But, outside the class, I have a problem when I try to test a probably not
    initialized class.
    For instance, think about this code:
    public void myFunc()
    {
    A a1;
    A a2;
    .....
    if (a1 == null)
    {
    //some null value handling
    }
    ....
    }

    The problem is that testing a1==null creates recursive calls to the operator
    == of class A.
    How do I test if something is null without having to invoke the == operator
    for that "something" ?


    Tnx in advance.


  • EL OSO

    #2
    Re: Overriding == and testing to null

    Dumb of me, I mixed two questions.
    What I was trying to say is that the invoked operator == for class A creates
    a null reference when it tries to access a2.x when a2 is null (as I wrote on
    MyFunc)

    I tried in the definition of operator == something like
    if (a1==null) or (a2==null)
    return false
    else
    return (a1.x == (a2.x)

    BUT, comparing a1 == null creates the recursive call to operator == in class
    A

    Do I make myself more clear this time? :p





    "EL OSO" <noone@nospam.c om> escribió en el mensaje
    news:OaE65zyYFH A.3280@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Hi, let's suppose I have a
    >
    > class A
    > {
    > int x;
    > ...
    > public static bool operator == (A a1 , A a2)
    > {
    > return (a1.x==a2.x);
    > }
    > }
    >
    > But, outside the class, I have a problem when I try to test a probably not
    > initialized class.
    > For instance, think about this code:
    > public void myFunc()
    > {
    > A a1;
    > A a2;
    > ....
    > if (a1 == null)
    > {
    > //some null value handling
    > }
    > ...
    > }
    >
    > The problem is that testing a1==null creates recursive calls to the[/color]
    operator[color=blue]
    > == of class A.
    > How do I test if something is null without having to invoke the ==[/color]
    operator[color=blue]
    > for that "something" ?
    >
    >
    > Tnx in advance.
    >
    >[/color]


    Comment

    • Mattias Sjögren

      #3
      Re: Overriding == and testing to null

      [color=blue]
      >BUT, comparing a1 == null creates the recursive call to operator == in class
      >A[/color]

      Cast to object first to prevent that.

      if ( (object)a1 == null )



      Mattias

      --
      Mattias Sjögren [MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.

      Comment

      • Helge Jensen

        #4
        Re: Overriding == and testing to null



        EL OSO wrote:
        [color=blue]
        > I tried in the definition of operator == something like
        > if (a1==null) or (a2==null)
        > return false
        > else
        > return (a1.x == (a2.x)[/color]

        That's not really what you want, is it, if (object)a1 == null &&
        (object)a2 == null ;)

        I use the pattern:

        public static bool operator==(A a1, A a2) {
        if ( (object)a1 == null )
        return (object)a2 == null;
        else if ((object)a2 == null)
        return false;
        else
        return a1.x == a2.x
        }
        [color=blue]
        > BUT, comparing a1 == null creates the recursive call to operator == in class
        > A[/color]

        Yes, you just overloaded operator==(A,A) , so that gets called to compare
        a1 and null.

        The "trick" is to make a1 staticly typed as something that compares by
        reference with operator==: object.
        [color=blue]
        > Do I make myself more clear this time? :p[/color]

        mostly,... I'm not that good at being clear myself.

        --
        Helge Jensen
        mailto:helge.je nsen@slog.dk
        sip:helge.jense n@slog.dk
        -=> Sebastian cover-music: http://ungdomshus.nu <=-

        Comment

        • Ted Miller

          #5
          Re: Overriding == and testing to null

          You could use object.Referenc eEquals().

          "EL OSO" <noone@nospam.c om> wrote in message
          news:OaE65zyYFH A.3280@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Hi, let's suppose I have a
          >
          > class A
          > {
          > int x;
          > ...
          > public static bool operator == (A a1 , A a2)
          > {
          > return (a1.x==a2.x);
          > }
          > }
          >
          > But, outside the class, I have a problem when I try to test a probably not
          > initialized class.
          > For instance, think about this code:
          > public void myFunc()
          > {
          > A a1;
          > A a2;
          > ....
          > if (a1 == null)
          > {
          > //some null value handling
          > }
          > ...
          > }
          >
          > The problem is that testing a1==null creates recursive calls to the
          > operator
          > == of class A.
          > How do I test if something is null without having to invoke the ==
          > operator
          > for that "something" ?
          >
          >
          > Tnx in advance.
          >
          >[/color]


          Comment

          Working...