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