Pass session to bool

Why can't I do this?

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

The compiler gives me:

Cannot convert type 'string' to 'bool'

Request["genericError"] must be an object, so why does the compiler consider it a string?

I'm looking for a reason for this, not how to get around it (using Convert )

+4
source share
4 answers

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

Assuming Request is an HttpRequest (as I suspect), indexer has a type string.

+4
source

Because it is a string. Try:

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

Even better,

use `bool.TryParse 'etc.

+3
source

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

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

+1
source

The value of the query variable is a string. This is not a session object (it will be Session ["genericError"]). Query variables are always IIRC strings.

0
source

Source: https://habr.com/ru/post/1335414/


All Articles