如何在operator == overload中测试null? [英] How to test for null in operator == overload?
问题描述
嗨:
我正在尝试调试以下代码,它会继续循环使用if
语句:
public static bool operator ==
(OnlineMemberNode first,OnlineMemberNode second)
{
if(first == null)
{
返回false;
}
...
}
我已将断点设置为if条件,并且它在那里继续循环
,最终导致堆栈溢出。我不明白在
我的测试中,第一个是AdminMode类型的对象(继承自
OnlineMemberNode)。
还有另外一种方式测试未定义的值。最初,
因为第二个参数是未定义的,当我调用成员
变量进行测试时,我得到一个空指针异常。
>
我认为问题在于每次我测试
null的第一个参数时,我都会递归调用自己来执行相同的测试。
但是,我真的不知道如何解决这个问题。
如何解决这个无限循环?
感谢任何提前帮助。
史蒂夫
如果你这样做会怎样?
>
if(first!= null)
{
if(second!= null)
{
//如果相同
返回true;
}
}
返回false;
-
使用M2,Opera的革命性电子邮件客户端: http://www.opera.com/m2/
if((object)first == null)
这个不应该进入无限递归。 />
Bruno。
" Steve Sargent" < I_hate_spammers@spammers_suck.com> écritdansle message
de news:40 ************* @ news.microsoft.com ...
嗨:
我正在尝试调试以下代码,并且它继续循环使用if
语句:
public static bool operator ==
( OnlineMemberNode首先,OnlineMemberNode第二个)
{
if(first == null)
{
返回false;
}
...
}
我已将断点设置为if条件,并且它会在那里循环,最终导致堆栈溢出。我不明白在我的测试中,第一个是AdminMode类型的对象(继承自OnlineMemberNode)。
是否有另一种方法来测试未定义的值。最初,因为第二个参数是未定义的,当我调用成员
变量进行测试时,我得到一个空指针异常。
我认为问题是每个我测试了
null的第一个参数,我递归地调用自己来执行相同的测试。
但是,我真的不知道如何解决这个问题。
如何解决这个无限循环?
感谢您提前提供任何帮助。
史蒂夫
>
如果你也超载,你会遇到同样的无限递归问题!=
(你应该这样做)。
你必须将第一个/第二个施放到物体上以迫使它使用默认的==或
!=而不是过载的物品。
Bruno。
" Morten Wennevik" <莫************ @ hotmail.com>一个écritdansle message de
news:op ************** @ msnews.microsoft.com ...会发生什么if you
if(first!= null)
{
if(second!= null)
{
//如果相同>返回true;
}
}
返回false;
-
使用M2,Opera的革命性电子邮件客户端: http://www.opera.com/m2/>
Hi:
I''m trying to debug the following code, and it keeps looping on the if
statement:
public static bool operator ==
(OnlineMemberNode first, OnlineMemberNode second)
{
if(first == null)
{
return false;
}
...
}
I''ve set my breakpoint to the if condition, and it keeps looping
there, eventually causing a stack overflow. I don''t understand. In
my testing, the first is an object of type AdminMode (inherits from
OnlineMemberNode).
Is there another way to test for an undefined value. Originally,
because the second parameter is undefined, when I call the member
variables to do the testing, I get a null pointer exception.
I think the problem is that each time I test the first parameter for
null, I''m recursively calling itself to perform the same test.
However, I don''t really know a way around this.
How do I get around this endless loop?
Thanks for any help in advance.
Steve
What happens if you do
if(first != null)
{
if(second != null)
{
//if same
return true;
}
}
return false;
--
Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/
Sounds normal to me. Did you try:
if ((object)first == null)
This one should not go into an infinite recursion.
Bruno.
"Steve Sargent" <I_hate_spammers@spammers_suck.com> a écrit dans le message
de news:40*************@news.microsoft.com...Hi:
I''m trying to debug the following code, and it keeps looping on the if
statement:
public static bool operator ==
(OnlineMemberNode first, OnlineMemberNode second)
{
if(first == null)
{
return false;
}
...
}
I''ve set my breakpoint to the if condition, and it keeps looping
there, eventually causing a stack overflow. I don''t understand. In
my testing, the first is an object of type AdminMode (inherits from
OnlineMemberNode).
Is there another way to test for an undefined value. Originally,
because the second parameter is undefined, when I call the member
variables to do the testing, I get a null pointer exception.
I think the problem is that each time I test the first parameter for
null, I''m recursively calling itself to perform the same test.
However, I don''t really know a way around this.
How do I get around this endless loop?
Thanks for any help in advance.
Steve
You''ll run into the same infinite recursion problem if you also overload !=
(and you should do it).
You have to cast first/second to object to force it to use the default == or
!= rather than the overloaded one.
Bruno.
"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de
news:op**************@msnews.microsoft.com...What happens if you do
if(first != null)
{
if(second != null)
{
//if same
return true;
}
}
return false;
--
Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/
这篇关于如何在operator == overload中测试null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!