覆盖==运算符的问题 [英] Problems overrideing the == operator

查看:71
本文介绍了覆盖==运算符的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁可以告诉我在下面的代码片段中我做错了什么?

我只是试图覆盖==运算符但没有运气。我的b $ b甚至不能让它达到==方法顶部的断点。我用来比较的

代码行是:


MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);

MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);

if(a == b)

{

...

}


请注意以下内容对我来说没问题:


if(a.Equals(b))< br $>
{

...

}


我不知道我是不是使用它不正确,因为==必须是静态的

或什么...


谢谢,


公共类MyState

{

private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;


public LINE_STATE PrimitiveState

{

get {return m_state; }

}


公共MyState(LINE_STATE状态)

{

m_state = state; < br $>
}


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

{

bool ret = false;


尝试

{

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

{

ret = state1.PrimitiveState ==

state2.PrimitiveState;

}

}

catch(exception ex)

{

LoggerUtil.LogError(MethodBase.GetCurrentMethod(),

ex.Message,ex.StackTrace);

}


返回ret;

}


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

{

return!(state1 == state2);

}


公共覆盖bool Equals(对象obj)

{

bool ret = false;

if(obj!= null&& obj is MyState)

{

ret = this.PrimitiveStat e ==

((MyState)obj)。PrimitiveState;

}

返回ret;

}


公共覆盖int GetHashCode()

{

返回base.GetHashCode();

}

}

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

推荐答案




有你试过


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

{

返回a.Equals(b) ;

}


应该有效。


HTH,

詹姆斯。


mathboy2写道:
Hi,

have you tried

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

that should work.

HTH,
James.

mathboy2 wrote:

有谁能告诉我在下面的代码片段中我做错了什么?

我只是试图覆盖==运算符但没有运气。我的b $ b甚至不能让它达到==方法顶部的断点。我用来比较的

代码行是:


MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);

MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);

if(a == b)

{

...

}


请注意以下内容对我来说没问题:


if(a.Equals(b))< br $>
{

...

}


我不知道我是不是使用它不正确,因为==必须是静态的

或什么...


谢谢,



公共类MyState

{

private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;


public LINE_STATE PrimitiveState

{

get {return m_state; }

}


公共MyState(LINE_STATE状态)

{

m_state = state; < br $>
}


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

{

bool ret = false;


尝试

{

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

{

ret = state1.PrimitiveState ==

state2.PrimitiveState;

}

}

catch(exception ex)

{

LoggerUtil.LogError(MethodBase.GetCurrentMethod(),

ex.Message,ex.StackTrace);

}


返回ret;

}


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

{

返回!(state1 == state2);

}


公共覆盖bool Equals(object obj)

{

bool ret = false;

if(obj!= null&& obj是MyState)

{

ret = this.PrimitiveState ==

((MyState)obj).PrimitiveState;

}

返回ret;

}


公共覆盖int GetHashCode()

{

返回base.GetHashCode();

}


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


这是一个好主意,但问题是我甚至无法获得线程

进入公共静态bool运算符==()方法第一个

的地方。逻辑是一样的,所以一旦我可以在那里得到它应该

只是工作...


Dan


pigeonrandle写道:
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

pigeonrandle wrote:




你试过吗


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

{

返回a.Equals(b);

}


应该有效。


HTH,

詹姆斯。


mathboy2写道:
Hi,

have you tried

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

that should work.

HTH,
James.

mathboy2 wrote:

有谁能告诉我在下面的代码片段中我做错了什么?

我只是简单试图覆盖==运算符但没有运气。我的b $ b甚至不能让它达到==方法顶部的断点。我用来比较的

代码行是:


MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);

MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);

if(a == b)

{

...

}


请注意以下内容对我来说没问题:


if(a.Equals(b))< br $>
{

...

}


我不知道我是不是使用它不正确,因为==必须是静态的

或什么...


谢谢,


公共类MyState

{

private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;


public LINE_STATE PrimitiveState

{

get {return m_state; }

}


公共MyState(LINE_STATE状态)

{

m_state = state; < br $>
}


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

{

bool ret = false;


尝试

{

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

{

ret = state1.PrimitiveState ==

state2.PrimitiveState;

}

}

catch(exception ex)

{

LoggerUtil.LogError(MethodBase.GetCurrentMethod(),

ex.Message,ex.StackTrace);

}


返回ret;

}


public static bool operator!=(MyState state1,MySt吃了州2)

{

返回!(state1 == state2);

}


public override bool Equals(object obj)

{

bool ret = false;

if(obj!= null&& obj是MyState)

{

ret = this.PrimitiveState ==

((MyState)obj).PrimitiveState;

}

返回ret;

}


公共覆盖int GetHashCode()

{

返回base.GetHashCode();

}

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




mathboy2写道:

mathboy2 wrote:

任何人都可以在下面的片段中告诉我我做错了什么代码?

我只是试图覆盖==运算符但没有运气。我的b $ b甚至不能让它达到==方法顶部的断点。我用来比较的

代码行是:


MyState a = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);

MyState b = new MyState(LINE_STATE.LINE_STATE_UNKNOWN);

if(a == b)

{

...

}


请注意以下内容对我来说没问题:


if(a.Equals(b))< br $>
{

...

}


我不知道我是不是使用它不正确,因为==必须是静态的

或什么...


谢谢,



公共类MyState

{

private LINE_STATE m_state = LINE_STATE.LINE_STATE_UNKNOWN;


public LINE_STATE PrimitiveState

{

get {return m_state; }

}


公共MyState(LINE_STATE状态)

{

m_state = state; < br $>
}


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

{

bool ret = false;


尝试

{

if(state1!= null&& state2!= null)
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)



嗯,您认为这会发生什么? state1!= null将调用

运算符!=

dereferences null和... splat。


试试这个:


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

{

bool ret = false;

尝试

{

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

{

ret = state1.PrimitiveState ==

state2.PrimitiveState;

}

}

catch(exception ex)

{

Console.Out.WriteLine(" Exception:" + ex.Message);

}

返回ret;

}


请记住,在Equals中,对象是TYPE对象,在!=和

==运营商

对象是TYPE MyState,递归到死。


Matt


这篇关于覆盖==运算符的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆