operator overloading

K

KK

Hi All
I'm new to .net framework and C#.I'm having an error in C#.Please help me

I have a class which overloaded = = and != operators as shown below

public class A
{
public static bool operator ==(A lhs,A rhs)
{
//Error checking here
if((null == lhs) || (null == rhs))
return false;
return lhs.Equals(rhs);
}
}
My code using this object is raising Stack Over flow exception as shown
below
A someA = new A();
if( someA == null)
{
//do some error handling thing here.
}

Thanks & Regards
Krishna
 
D

Dennis Myrén

I think your problem is due to recursive calls to ==
because this code:
//Error checking here
if((null == lhs) || (null == rhs))
return false;
actually calls itself to check the equality.
You need to cast lhs and rhs to System.Object in that if statement;
//Error checking here
if(null == (object) lhs || null == (object) rhs)
return false;

That should work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top