test reference type for null?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Oliver

    #1

    test reference type for null?

    Hi,

    When I test my user defined class type passed parameter variable for null
    it throws an exception. If I use class "string" the null test doesn't
    through an exception. What could I be doing in my class that causes the
    exception?

    Thanks,
    Mark

    Thanks,
    Mark




  • Peter Koen

    #2
    Re: test reference type for null?

    "Mark Oliver" <mark_oliver@co mcast.net> wrote in
    news:AqXlb.1131 3$e01.21529@att bi_s02:
    [color=blue]
    > Hi,
    >
    > When I test my user defined class type passed parameter variable
    > for null
    > it throws an exception. If I use class "string" the null test
    > doesn't through an exception. What could I be doing in my class that
    > causes the exception?
    >
    > Thanks,
    > Mark
    >
    > Thanks,
    > Mark
    >
    >
    >
    >
    >[/color]

    the only chance that your custom class can throw an exception on
    myobj==null is when you've overriden the equals method (or the == operator)

    maybe you should check your class implementation

    --
    best regards

    Peter Koen
    -----------------------------------
    MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: test reference type for null?

      Mark Oliver <mark_oliver@co mcast.net> wrote:[color=blue]
      > When I test my user defined class type passed parameter variable for null
      > it throws an exception. If I use class "string" the null test doesn't
      > through an exception. What could I be doing in my class that causes the
      > exception?[/color]

      Chances are you've overridden == in some way that assumes that both
      arguments are non-null (e.g. calling firstArgument.E quals
      (secondArgument )). If you make the first few lines of your operator ==
      override:

      if ((Object)first= =(Object)second )
      return true;

      if ((Object)first= =null || (Object)second= =null)
      return false;

      and then do whatever else you were doing, you should be fine.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Mark Oliver

        #4
        Re: test reference type for null?

        Thanks, That was the problem.
        Mark

        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
        news:MPG.1a02ae 462cb7dc8e98992 9@msnews.micros oft.com...[color=blue]
        > Mark Oliver <mark_oliver@co mcast.net> wrote:[color=green]
        > > When I test my user defined class type passed parameter variable for[/color][/color]
        null[color=blue][color=green]
        > > it throws an exception. If I use class "string" the null test doesn't
        > > through an exception. What could I be doing in my class that causes[/color][/color]
        the[color=blue][color=green]
        > > exception?[/color]
        >
        > Chances are you've overridden == in some way that assumes that both
        > arguments are non-null (e.g. calling firstArgument.E quals
        > (secondArgument )). If you make the first few lines of your operator ==
        > override:
        >
        > if ((Object)first= =(Object)second )
        > return true;
        >
        > if ((Object)first= =null || (Object)second= =null)
        > return false;
        >
        > and then do whatever else you were doing, you should be fine.
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet
        > If replying to the group, please do not mail me too[/color]


        Comment

        Working...