ASP.NET C# Bool 类型转换

发布于 2024-12-08 20:31:52 字数 643 浏览 1 评论 0原文

此代码抛出错误:

    bool status1 = (bool)Cache["cache_req_head"];
    bool status2 = (bool)Cache["cache_super"];
    bool status3 = (bool)Cache["cache_head"];

这就是缓存变量的设置方式:

if (checkreqhead == true)
        {
            Cache["cache_req_head"] = true;
        }
        else if (checksuper == true)
        {
            Cache["cache_super"] = true;
        }
        else if (checkhead == true)
        {
            Cache["cache_head"] = true;
        }

来自 PHP 背景,这很尴尬。错误是:

未将对象引用设置为对象的实例

我确信这确实很简单,但我可能无法发现它。

感谢大家的帮助:)

This code throwing out an error:

    bool status1 = (bool)Cache["cache_req_head"];
    bool status2 = (bool)Cache["cache_super"];
    bool status3 = (bool)Cache["cache_head"];

This is how the cache variables were set:

if (checkreqhead == true)
        {
            Cache["cache_req_head"] = true;
        }
        else if (checksuper == true)
        {
            Cache["cache_super"] = true;
        }
        else if (checkhead == true)
        {
            Cache["cache_head"] = true;
        }

Coming from PHP background, this is awkward. The error is:

Object reference not set to an instance of an object

I'm certain it is something really simple, but probably I can't spot it.

THANKS ALL FOR HELPING :)

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

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

发布评论

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

评论(4

微凉徒眸意 2024-12-15 20:31:52

“对象引用未设置到对象的实例”是 c# 行话,意思是“你用 null 值做了一些愚蠢的事情”

如果缓存是空的,你需要检查首先

bool status1 = (bool)Cache["cache_req_head"];

应该是

bool status1 = false;
if (Cache["cache_req_head"] != null)
{
   status1 = (bool)Cache["cache_req_head"];
}

这是一个效果事实上,c# 中的值类型(如 bool、int 等)不能为 null。如果您希望值类型允许空值,可以使用一个包装器 Nullable ,其简写形式为 T?

您可以将值转换为 bool?,因为这允许 null

bool? status1 = (bool?)Cache["cache_req_head"];

然后,您可以检查 status1 == nullstatus1.HasValue,以获取实际的 bool 值,您需要使用 status1.Value 来挑选它。如果您在 status1 == null 时选择 status1.Value,您将得到一个运行时异常,就像您刚刚得到的那样。

"Object reference not set to an instance of an object" is c# lingo for "you did something stupid with a null value"

If the Cache is empty you need to check that first

bool status1 = (bool)Cache["cache_req_head"];

should be

bool status1 = false;
if (Cache["cache_req_head"] != null)
{
   status1 = (bool)Cache["cache_req_head"];
}

This is an effect of the fact that value types (like bool, int, etc) in c# can not be null. There is a wrapper, Nullable<T> with the shorthand T? that you can use if you want to allow null values for the value types.

You can cast your value to a bool? since that allows for null.

bool? status1 = (bool?)Cache["cache_req_head"];

You can then check status1 == null or status1.HasValue, to get the actual bool value you need to pick it out with status1.Value. If you pick status1.Value while status1 == null you will get a runtime exception like the one you just got.

恰似旧人归 2024-12-15 20:31:52

实际上,检查 Cache 中是否存在值的最佳方法是执行以下

//string is used as an example; you should put the type you expect
string variable = Cache["KEY"] as string;

if(variable!=null)
{
  // do something
}

操作: if(Cache["KEY"]!=null) myVariable=Cache[ "Key"]; 是不安全的,因为存储在 Cache["Key"] 中的对象可能会在您有机会分配之前从 Cache 中删除它到 myVariable 你最终会想myVariable 持有非空值。

Actually, the best way to check for whether a value exists or not in Cache is by doing:

//string is used as an example; you should put the type you expect
string variable = Cache["KEY"] as string;

if(variable!=null)
{
  // do something
}

The reason why doing if(Cache["KEY"]!=null) myVariable=Cache["Key"]; is unsafe, is because the object stored in Cache["Key"] may be removed from Cache before you get a chance to assign it to myVariable and you end up thinking that myVariable holds a non-null value.

无声情话 2024-12-15 20:31:52

显然,您一次只能设置一个缓存条目。因此,除非您运行“setter”代码 3 次,并且只有 1 个变量设置为 true,否则您总是会返回 null。
null 不会转换为 bool,因为它是值类型。尝试使用 bool?

You obviously only setting one of the cache entries at a time. So unless you run the "setter" code 3 times with only 1 variable set to true, then you always going to have nulls returned.
null does not cast into bool because its a value type. Try using bool?

秋叶绚丽 2024-12-15 20:31:52

由于 Cache[] 返回一个对象,如果未设置该对象,则该对象为 null,因此在尝试将 null 转换为 bool 时会出现异常。

您必须首先检查该键是否存在,否则您必须将每个键设置为“false”作为默认值。

Since Cache[] returns an object, which is null if not set, then you're getting an exception trying to cast null into a bool.

You'd have to check if that key exists first, or you'd have to set each key to "false" as a default.

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