Boolean

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Boolean

    Hi,

    I have the following code line:
    (string)ViewDat a["Tags"] ?? ViewData.Model. Tags.ToString()

    How can I do something similar for a boolean type variable. I tried:
    (bool)ViewData["IsPublishe d"] ?? ViewData.Model. IsPublished

    However it says ?? cannot be used with boolean type variable.

    Thanks,
    Miguel
  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Boolean

    shapper wrote:
    I have the following code line:
    (string)ViewDat a["Tags"] ?? ViewData.Model. Tags.ToString()
    >
    How can I do something similar for a boolean type variable. I tried:
    (bool)ViewData["IsPublishe d"] ?? ViewData.Model. IsPublished
    >
    However it says ?? cannot be used with boolean type variable.
    Because bool can not be null then the ?? operation does
    not make much sense.

    Arne

    Comment

    • Hans Kesting

      #3
      Re: Boolean

      shapper wrote on 21-8-2008 :
      Hi,
      >
      I have the following code line:
      (string)ViewDat a["Tags"] ?? ViewData.Model. Tags.ToString()
      >
      How can I do something similar for a boolean type variable. I tried:
      (bool)ViewData["IsPublishe d"] ?? ViewData.Model. IsPublished
      >
      However it says ?? cannot be used with boolean type variable.
      >
      Thanks,
      Miguel
      Try this:
      ViewData["IsPublishe d"] as bool? ?? ViewData.Model. IsPublished

      the "as bool?" part tries to cast ViewData["IsPublishe d"] to a nullable
      bool, which will return null (instead of a cast exception) on failure.
      The "??" part will convert the null to the result of IsPublished.
      Note: the result-type of this expression is "bool" (assuming
      IsPublished returns a bool) and not "bool?".

      Hans Kesting


      Comment

      Working...