English 中文(简体)
ASP.NET C# Bool型式投放
原标题:ASP.NET C# Bool type casting

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"];

就是如何确定海滩变量:

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:

不针对物体的事例提出反对

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

能力建设:

最佳回答

“未提及物体的标的”为c# lingo,“在座标有<代码>null数值”

如果Cache是空洞的,那么首先需要检查。

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

应当

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

这是因为,在c#中,价值类型(如ool、 in等)不能无效。 有一个包装,Nullable<T> with the shorthand T?, 您如果想允许价值类型的无效价值,就可以使用。

您可以把自己的价值提升到<代码>bool?,因为这一条允许<代码>null。

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

然后,可以核对<代码>status1 = 无或status1.HasValue,以便获得你需要用<代码>status1”取出的实际保值。 数值。 请参看<代码>status1。 数值,status1 = 无 你们将获得像你刚才那样的暂时例外。

问题回答

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
}

为什么做if(Cache[“KEY”]!=null) myVariable=Cache[“Key”];是不安全的,因为存储在Cache[“Key”]上的物体,在有机会指定其为myVariable之前,可以从/code>上删除。

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?

由于Cache[]返回一个物体,如果该物体不设,则该物体无效,那么你就重新获得一个例外,试图将它变成一个ool。

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!