O'Reilly Forums: Cannot implicitly convert type 'bool?' to 'bool' - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   mohi.sillah 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 09-April 08

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!
0

#2 User is offline   lostre 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 85
  • Joined: 10-December 07
  • 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 ?
0

#3 User is offline   jskeet 

  • Active Member
  • PipPipPipPip
  • Group: Members
  • Posts: 144
  • Joined: 22-March 08

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
0

#4 User is offline   lostre 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 85
  • Joined: 10-December 07
  • 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
0

#5 User is offline   jskeet 

  • Active Member
  • PipPipPipPip
  • Group: Members
  • Posts: 144
  • Joined: 22-March 08

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
0

#6 User is offline   lostre 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 85
  • Joined: 10-December 07
  • Location:Greece-Germany-UK

Posted 11 April 2008 - 06:50 AM

oups !! xexexe wink.gif
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users