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
 
Ad

Advertisements

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

Similar Threads

operator overloade? 5
Operator Overloading? 4
overload for "operator == " 1
overloading operator== 4
Operator Overloading 1
Operator overloading 3
Overloading operator++ 4
operator overloading ! 0

Top