2

I'm trying to do something like:

        bool? Verified;
        Verified = Request.QueryString["verifed"]==null
? null :bool.Parse(Request.QueryString["verifed"]);

But I'm getting the error:

Type of conditional expression cannot be determined because there is no implicit conversion between <null> and bool

Is there a simple single line way to do this, rather than doing it like:

if (Request.QueryString["confirmed"] == null)
    Confirmed = null;
else
    Confirmed = bool.Parse(Request.QueryString["confirmed"]);
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171

4 Answers4

3

You get error because compiler tries to use same return type for ?: operator (which is bool).

Because you can't convert null to bool, you can convert bool to bool? instead:

Verified = Request.QueryString["verifed"] == null ? null :
   (bool?)bool.Parse(Request.QueryString["verifed"]);
Sinatr
  • 20,892
  • 15
  • 90
  • 319
0

Use default keyword;

bool? Verified;
Verified = Request.QueryString["verifed"] == null ? default(bool?) : bool.Parse(Request.QueryString["verifed"]);

Result of default(bool?) is null if someone not sure about it.

Need more on this. Read.

What is the use of `default` keyword in C#?
What does default(object); do in C#?

Community
  • 1
  • 1
Irshad
  • 3,071
  • 5
  • 30
  • 51
0

I think your query param is empty string and because of that you receive this error on conversion. Also why use nullable bool, just put false when the value is null or "".

bool initialValue = false;
bool verified = Request.QueryString["verifed"]==null || Request.QueryString["verifed"] == "" ? false:bool.TryParse(Request.QueryString["verifed"], out initialValue);
mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • the reason I have declared bool? is that null is also an acceptable option, your solution ignores null value converting it to false – Ashkan Mobayen Khiabani Feb 03 '16 at 09:03
  • It depends what you want to do when you receive null, if you want to say it is not verified, so use it false. Why make your logic complicated ? – mybirthname Feb 03 '16 at 09:05
  • it is used in a query like this: `User.Where(u=> Verified==null || Verified == u.Verified);` so I can search all users, only those which are verified or only those which are not – Ashkan Mobayen Khiabani Feb 03 '16 at 09:06
-2

Change this:

Verified = Request.QueryString["verifed"]==null? null :bool.Parse(Request.QueryString["verifed"]);

to this:

Verified = Request.QueryString["verifed"]==null? (bool?)null :bool.Parse(Request.QueryString["verifed"]);

Simply example of this is:

        bool test = true;

        var test = 1 == 1 ? true : false; // Good

        bool? test2 = true;

        var test2 = 1 == 1 ? (bool?)null : false; // Good

       var test2 = 1 == 1 ? null : false; // Error: Type of conditional expression cannot be determined because there is no implicit conversion between <null> and bool

More info:

C# : Implicit conversion between '<null>' and 'bool'

In C# why can't a conditional operator implicitly cast to a nullable type

Community
  • 1
  • 1
blogprogramisty.net
  • 1,714
  • 1
  • 18
  • 22