Jump to content


Photo

Cannot implicitly convert type 'bool?' to 'bool'


  • Please log in to reply
5 replies to this topic

#1 mohi.sillah

mohi.sillah

    New Member

  • Members
  • Pip
  • 1 posts

Posted 09 April 2008 - 10:51 PM

Hello All,
I am experiencing a "cannot implicitly convert type 'bool?'" error. Below is the code that sets sopdata to obsolete if found is not null.

ManagerService.ComponentVO found = GetSOP(queueItem);

if (found != null)
{
sopdata.Obsolete = found.notActive; //.notActive
sopdata.RevisionDate = found.revisionDate.datePlusTime;
}

Please help me!

#2 lostre

lostre

    Active Member

  • Members
  • PipPipPip
  • 85 posts
  • Location:Greece-Germany-UK

Posted 10 April 2008 - 02:13 AM

so you have a value object that gets its values from the GetSOP method
and then you want to check if the reference is not null.

the code seems right to my beginner eyes.. can you give the exact error message info ?

#3 jskeet

jskeet

    Active Member

  • Members
  • PipPipPipPip
  • 144 posts

Posted 10 April 2008 - 02:50 PM

(mohi.sillah)
Hello All,
I am experiencing a "cannot implicitly convert type 'bool?'" error. Below is the code that sets sopdata to obsolete if found is not null.

ManagerService.ComponentVO found = GetSOP(queueItem);

if (found != null)
{
sopdata.Obsolete = found.notActive; //.notActive
sopdata.RevisionDate = found.revisionDate.datePlusTime;
}

Please help me!


My guess is that ComponentVO.notActive is a "bool?" (which means Nullable<bool> - effectively a "yes/no/maybe" value) whereas sopdata.Obsolete is a bool (i.e. yes/no).

However, we'd need to know exactly which line the error occurred on - as well as the rest of the type information.

If the situation is as I guessed, you'll need to work out what to do if found.notActive is null (i.e. the "maybe" value).

Jon

#4 lostre

lostre

    Active Member

  • Members
  • PipPipPip
  • 85 posts
  • Location:Greece-Germany-UK

Posted 11 April 2008 - 01:20 AM

jon how great is this
you always seem to post when i am trying to figure out something wink.gif

i was just trying to do something with "magic numbers" (giving a -1 initial value) and i came across nullable types...

anything you want to share about it (like when to use them how what they are etc) if you find any free time would be great! wink.gif)

cheers
Nik

#5 jskeet

jskeet

    Active Member

  • Members
  • PipPipPipPip
  • 144 posts

Posted 11 April 2008 - 06:31 AM

(lostre)
jon how great is this
you always seem to post when i am trying to figure out something wink.gif


smile.gif

(lostre)
i was just trying to do something with "magic numbers" (giving a -1 initial value) and i came across nullable types...

anything you want to share about it (like when to use them how what they are etc) if you find any free time would be great! wink.gif)


Well, I don't have much time to go into them in detail, but I would urge caution when using them. They can be really useful, and C# has nice syntax around them (so you can compare them with null or set a value to null, even though it's definitely not the null reference which the null keyword usually means). However, if it's not really obvious that they're involved, life can get confusing (as witnessed by the post).

To do a really small plug for my book, the whole of chapter 4 is dedicated to discussing nullable types. However, this forum's about HFC# rather than my book, so if you want to know any more about what's covered I suggest you email/PM me smile.gif

Jon

#6 lostre

lostre

    Active Member

  • Members
  • PipPipPip
  • 85 posts
  • Location:Greece-Germany-UK

Posted 11 April 2008 - 06:50 AM

oups !! xexexe wink.gif




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users