ASP.NET C# Bool 类型转换
此代码抛出错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“对象引用未设置到对象的实例”是 c# 行话,意思是“你用
null
值做了一些愚蠢的事情”如果缓存是空的,你需要检查首先
应该是
这是一个效果事实上,c# 中的值类型(如 bool、int 等)不能为 null。如果您希望值类型允许空值,可以使用一个包装器
Nullable
,其简写形式为T?
。您可以将值转换为
bool?
,因为这允许null
。然后,您可以检查
status1 == null
或status1.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
should be
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 shorthandT?
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 fornull
.You can then check
status1 == null
orstatus1.HasValue
, to get the actual bool value you need to pick it out withstatus1.Value
. If you pickstatus1.Value
whilestatus1 == null
you will get a runtime exception like the one you just got.实际上,检查
Cache
中是否存在值的最佳方法是执行以下操作:
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:The reason why doing
if(Cache["KEY"]!=null) myVariable=Cache["Key"];
is unsafe, is because the object stored inCache["Key"]
may be removed fromCache
before you get a chance to assign it tomyVariable
and you end up thinking that myVariable holds a non-null value.显然,您一次只能设置一个缓存条目。因此,除非您运行“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 intobool
because its a value type. Try usingbool?
由于 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.