Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast session variable 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 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)

like image 555
m.edmondson Avatar asked Feb 26 '23 09:02

m.edmondson


2 Answers

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.

like image 180
Jon Skeet Avatar answered Mar 07 '23 03:03

Jon Skeet


Because it is a string. Try:

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

Better yet,

use `bool.TryParse' etc ...

like image 38
Lill Lansey Avatar answered Mar 07 '23 04:03

Lill Lansey