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();
}
}
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();
}
}