开发者

Cast session variable to bool

开发者 https://www.devze.com 2023-02-05 01:07 出处:网络
Why can\'t I do this? if ((bool)Request[\"genericError\"] != true) { return; } Compiler gives me: Cannot convert type \'string\' to \'bool\'

Why can't I do this?

if ((bool)Request["genericError"] != true)
   {
       return;
   }

Compiler gives me:

Cannot convert type 'string' to 'bool'

Request["genericError"] should be an开发者_Go百科 object, so why does the compiler think its a string?

I'm looking for the reason for this, not how to sidestep it (using Convert)


What makes you think that Request["genericError"] should be an object?

Assuming Request is an HttpRequest (as I suspect), the indexer is of type string.


Because it is a string. Try:

if ( bool.parse (Request["genericError"] ) != true)  return;

Better yet,

use `bool.TryParse' etc ...


In .NET a NameValueCollection is defined as: Represents a collection of associated String keys and String values that can be accessed either with the key or with the index.

http://msdn.microsoft.com/en-US/library/system.collections.specialized.namevaluecollection(v=VS.80).aspx


The value of the request variable is a string. It isn't a session object (that would be Session["genericError"]). Request variables are always strings IIRC.

0

精彩评论

暂无评论...
验证码 换一张
取 消