simple operator overload question

  • Thread starter Thread starter Norvin Laudon
  • Start date Start date
N

Norvin Laudon

Hi,

Should be simple, but I can't seem to get a handle on it.

I've over-ridden the == operator in my class.
public static bool operator==(XrayImage lhs, XrayImage rhs)

{

return ( (lhs.name == rhs.name) && (lhs.imageTimeDateStamp ==
rhs.imageTimeDateStamp) );

}

but now I can't compare a class instance to null! I get an error thrown in
my == function: "Object reference not set to an instance of an object."

i.e.:

XrayImage x;

if (x == null)



Thanks,

Norvin
 
Norvin,

If you want to consider nulls to be equal:

public static bool operator ==(XrayImage lhs, XrayImage rhs)
{
bool retVal = false;

if ((lhs == null) && (rhs == null))
{
retVal = true;
}
else
{
if (!((lhs == null) || (rhs == null)))
{
retVal = ((lhs.name == rhs.name) && (lhs.imageTimeDateStamp ==
rhs.imageTimeDateStamp));
}
}

return retVal;
}

If you don't want to consider nulls to be equal:

public static bool operator ==(XrayImage lhs, XrayImage rhs)
{
bool retVal = false;

if (!((lhs == null) || (rhs == null)))
{
retVal = ((lhs.name == rhs.name) && (lhs.imageTimeDateStamp ==
rhs.imageTimeDateStamp));
}

return retVal;
}

HTH,
Nicole
 
Back
Top