test reference type for null?

  • Thread starter Thread starter Mark Oliver
  • Start date Start date
M

Mark Oliver

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
 
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

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
 
Mark Oliver said:
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?

Chances are you've overridden == in some way that assumes that both
arguments are non-null (e.g. calling firstArgument.Equals
(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.
 
Thanks, That was the problem.
Mark

Jon Skeet said:
Chances are you've overridden == in some way that assumes that both
arguments are non-null (e.g. calling firstArgument.Equals
(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.
 
Back
Top