与 null 比较时的奇怪行为
我有一个 gridview 从对象数据源获取数据。对象数据源正在从一种采用一个参数(字符串)的方法获取数据。该参数使用查询字符串从页面 url 提供,默认值设置为 null。在接受参数的方法中,我试图检查参数是否为空返回所有数据,否则返回数据 从参数中获取 id - 例如。代码。
public list<string> MyMethod (string param)
{
if(param == null)
{
return // all
}
else
{
return // id with param
}
}
我尝试调试程序,参数实际上为空,但 if () 语句始终处于“false”状态。即 param == null 总是评估为 false。我尝试了 String.IsNullorEmpty(param),但它的计算结果仍然为 false。我不明白问题是什么。请帮忙。 非常感谢。
I have a gridview taking data from object data source. The object data source is taking data from a method which takes one parameter (string). The parameter is supplied from page url using querystring, and the default value is set to null. In the method taking parameter, I am trying to check if the parameter is null return all data else return data
with id got from the parameter - eg. code.
public list<string> MyMethod (string param)
{
if(param == null)
{
return // all
}
else
{
return // id with param
}
}
I tried to debug the program and the param is actually null but the if () statement is always going to "false" condition. i.e. param == null always evaluating to false. I tried String.IsNullorEmpty(param), and it still is evaluating to false. I don't understand what the problem is. Please help.
Many Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你绝对确定该对象为空吗,空字符串和空对象之间有很大的区别!
Are you absolutely sure the object is null, there is a big difference between an empty string and a null object!!
添加屏幕截图后,我们可以看到您传递给方法的
"null"
参数而不是null
。我不熟悉 QueryString 但也许这会有所帮助:
如果您使用 QueryStringParameter,您可以设置这些属性以获取
null
而不是"null"
:您还需要设置
After you added screenshot now we can see that you passing to method
"null"
parameter notnull
.I'am not familiar with QueryString but maybe this helps:
If you using QueryStringParameter you can set these properties to get
null
instead"null"
:also you need to set
尝试:
使用此语法,您还可以将字符串设置为
""
。Try:
With this syntax you can also set your string to
""
.您是否尝试过单步执行和调试(假设您使用的是 Visual Studio)?
在方法签名上放置一个断点,然后将鼠标悬停在 param 变量上,以查看每个步骤后它的值是什么。
调试将帮助您快速识别此类问题的根源。
编辑
您添加的屏幕截图显示您的 param 变量等于字符串“null”,而不是
null
。您可以将 if 语句更改为 if(param == "null") ,这应该可以工作,但真正的解决方案很可能根本不使用字符串“null”,因此无论您在何处分配您传递给函数的变量。
另外,您的代码示例是 param == null 但您的屏幕截图是 param != null。 != 表示不等于,我不确定这是否只是一个拼写错误或者您没有注意到。
Have you tried stepping through and debugging (assuming you're using visual studio)?
Put a breakpoint on your method signature and then hover over with your mouse on the param variable to see what it's value is after each step.
Debugging will help you identify the source of issues like this pretty fast.
Edit
The screenshot you added shows your param variable is equal to the string "null", not
null
.You can change your if statement to if(param == "null") and that should work, but the real fix for this is most likely to not use the string "null" at all so that'll require editing wherever you assign the variable you pass to the function.
Also, your code sample is param == null yet your screenshot is param != null. The != means not equal, I'm not sure if that's just a typo or you haven't noticed.
让我猜一下。您的输入参数
param
可能有"null"
而不是null
。例如,在您的情况下编辑
建议:
将
param
添加到监视窗口并检查值是什么。我很确定在你的情况下它不是null
。Let me guess. It could be the case that your input parameter
param
has"null"
and notnull
. For example, in your caseEdit
Suggesstion:
add
param
to watch window and check what's the value. I'm pretty sure in your case it is something else thannull
.