与 null 比较时的奇怪行为

发布于 2024-12-08 23:48:31 字数 564 浏览 3 评论 0原文

我有一个 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.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青芜 2024-12-15 23:48:32

你绝对确定该对象为空吗,空字符串和空对象之间有很大的区别!

if(String.IsNullorEmpty(param))
{
return list;
}
else
{
return list.findIndex(param);
}

Are you absolutely sure the object is null, there is a big difference between an empty string and a null object!!

if(String.IsNullorEmpty(param))
{
return list;
}
else
{
return list.findIndex(param);
}
少女净妖师 2024-12-15 23:48:32

添加屏幕截图后,我们可以看到您传递给方法的 "null" 参数而不是 null

我不熟悉 QueryString 但也许这会有所帮助:

如果您使用 QueryStringParameter,您可以设置这些属性以获取 null 而不是 "null"

<asp:QueryStringParameter DefaultValue="" ConvertEmptyStringToNull="True" />

您还需要设置

<asp:SqlDataSource CancelSelectOnNullParameter="False" />

After you added screenshot now we can see that you passing to method "null" parameter not null.

I'am not familiar with QueryString but maybe this helps:

If you using QueryStringParameter you can set these properties to get null instead "null":

<asp:QueryStringParameter DefaultValue="" ConvertEmptyStringToNull="True" />

also you need to set

<asp:SqlDataSource CancelSelectOnNullParameter="False" />
林空鹿饮溪 2024-12-15 23:48:32

尝试:

if (String.IsNullOrEmpty(param))
{
}

使用此语法,您还可以将字符串设置为 ""

Try:

if (String.IsNullOrEmpty(param))
{
}

With this syntax you can also set your string to "".

终难愈 2024-12-15 23:48:31

您是否尝试过单步执行和调试(假设您使用的是 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.

谜泪 2024-12-15 23:48:31

让我猜一下。您的输入参数 param 可能有 "null" 而不是 null。例如,在您的情况下

if (param == null) // you're comparing "null" == null which will always be false.

编辑

建议:
param 添加到监视窗口并检查值是什么。我很确定在你的情况下它不是 null

Let me guess. It could be the case that your input parameter param has "null" and not null. For example, in your case

if (param == null) // you're comparing "null" == null which will always be false.

Edit

Suggesstion:
add param to watch window and check what's the value. I'm pretty sure in your case it is something else than null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文