Problems overrideing the == operator

M

mathboy2

Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:

MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}

Note that the following works fine for me:

if (a.Equals(b))
{
...
}

I don't know if I'm using it incorrectly because the == must be static
or what...

Thanks,
Dan


public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;

public LINE_STATE PrimitiveState
{
get { return m_state; }
}

public MyState(LINE_STATE state)
{
m_state = state;
}

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;

try
{
if (state1 != null && state2 != null)
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
LoggerUtil.LogError(MethodBase.GetCurrentMethod(),
ex.Message, ex.StackTrace);
}

return ret;
}

public static bool operator !=(MyState state1, MyState state2)
{
return !(state1 == state2);
}

public override bool Equals(object obj)
{
bool ret = false;
if (obj != null && obj is MyState)
{
ret = this.PrimitiveState ==
((MyState)obj).PrimitiveState;
}
return ret;
}

public override int GetHashCode()
{
return base.GetHashCode();
}


}
 
P

pigeonrandle

Hi,

have you tried

public static bool operator ==(MyState state1, MyState state2)
{
return a.Equals(b);
}

that should work.

HTH,
James.
 
M

mathboy2

It's a good idea, but the problem is that I can't even get the thread
to enter the public static bool operator ==() method in the first
place. The logic is the same so once I can get it in there it should
just work...

Dan
 
M

Matt

mathboy2 said:
Can anyone tell me what I'm doing wrong in the below snippet of code?
I'm simply trying to override the == operator but having no luck. I
can't even get it to hit a break point at the top of the == method. The
line of code I'm using to compare is:

MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);
if (a == b)
{
...
}

Note that the following works fine for me:

if (a.Equals(b))
{
...
}

I don't know if I'm using it incorrectly because the == must be static
or what...

Thanks,
Dan


public class MyState
{
private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;

public LINE_STATE PrimitiveState
{
get { return m_state; }
}

public MyState(LINE_STATE state)
{
m_state = state;
}

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;

try
{
if (state1 != null && state2 != null)

Um, what do you think is going to happen here? state1 != null will call
operator!= which
dereferences null and... splat.

Try this:

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;


try
{
if (!state1.Equals(null)&& !state2.Equals(null) )
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
}
catch (Exception ex)
{
Console.Out.WriteLine("Exception: " + ex.Message);
}


return ret;
}

Remember, while in Equals the object is of TYPE object, in the != and
== operators
the object is of TYPE MyState, recursing itself to death.

Matt
 
P

pigeonrandle

Have you tried taking the 'static' out? It might be something to do
with c# defaulting to object==object since the other members of your
class are not static.
I can't say i've tried this in c#, but if i say that it's impossible(!)
then someone is sure to correct me...

Good luck,
James.
 
M

mathboy2

Matt,

A very valid point. This is something obviously wrong with my logic and
I'll have to deal with that... but it doesn't solve the main
problem....

I can change my code to what is below and I still don't hit a break
point at the top of operator == overload.

Thanks,
Dan


public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;


try
{
ret = state1.PrimitiveState == state2.PrimitiveState;
}
catch (Exception ex)
{
Console.Out.WriteLine("Exception: " + ex.Message);
}


return ret;
}
 
M

mathboy2

sorry,

here is the code:

public static bool operator ==(MyState state1, MyState state2)

{
bool ret = false;

try
{
ret = state1.PrimitiveState ==
state2.PrimitiveState;
}
catch (Exception ex)
{
Console.Out.WriteLine("Exception: " + ex.Message);
}

return ret;
}
 
M

Matt

mathboy2 said:
Matt,

A very valid point. This is something obviously wrong with my logic and
I'll have to deal with that... but it doesn't solve the main
problem....

I can change my code to what is below and I still don't hit a break
point at the top of operator == overload.

Rather odd. I copied your code into a test program, and implemented
LINE_STATE as a simple enum. My test crashed the first time, due to
the issue I mentioned. When I changed it, it ran fine and the objects
compared fine.

Could it be that you are simply running into a weird debugger issue,
rather than a code problem? Try just pulling the class into a separate
test program and see if you see what I do.

Matt
 
M

mathboy2

Matt,

You're right! It worked perfectly in a new test app. Hmmm... well
obviously something is very different in my large environment, but at
least I can track it down now that I know that I'm not doing something
wrong with the == overload!

Thanks a bunch for you help!

Dan
 
B

Ben Voigt

Matt said:
Um, what do you think is going to happen here? state1 != null will call
operator!= which
dereferences null and... splat.

His operator== tries to be careful not to dereference null. However, the
infinite recursion really is a problem. That's trivial to fix, however,
since the current operator== defines null as not equal to anything, even
null, the if condition will always succeeds, null is dereferenced after all,
and splat.
Try this:

public static bool operator ==(MyState state1, MyState state2)
{
bool ret = false;


try
{
if (!state1.Equals(null)&& !state2.Equals(null) )

Rather object.ReferenceEquals(state1, null) or just null == (object)state1
 
D

Dave Sexton

Hi Dan,

Just FYI:

You should think about making your class a struct since you're giving it
value-type semantics.

If I needed to use an instance of your class in my code, I would assume
reference-equality, which would make my life difficult if I wanted to use it
in a Dictionary since two completely different instances might return the
same value, unbeknownst to me.
 
C

Christof Nordiek

Hi,

for preventing recursion you could cast one of the operands to object:
instead of

if (state1 != null && state2 != null)

you also could use:

if ((objecj)state1!=null && (object)state2!=null)

maybe more readable than:

if (!state1.Equals(null)&& !state2.Equals(null) )
 

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