471,334 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

Casting null values

Hi,

Im having a bit of difficulty working out how to do the following.
method ( paramType? param1, DateTime? requiredByDate , paramType? param3)
I have a row property which I need to pass to this method, this has a
property of IsNull which is a boolean, so I tried to do the following

method ( param1,
row.IsRequiredByDateNull() ? null, row.RequiredByDate,
param3 )

This fails on compile because it cant cast from a null to a DateTime.

How can i construct this ?
Jun 27 '08 #1
5 1508
Microsoft Newsserver <me@nowhere.comwrote:
Im having a bit of difficulty working out how to do the following.
method ( paramType? param1, DateTime? requiredByDate , paramType? param3)
I have a row property which I need to pass to this method, this has a
property of IsNull which is a boolean, so I tried to do the following

method ( param1,
row.IsRequiredByDateNull() ? null, row.RequiredByDate,
param3 )

This fails on compile because it cant cast from a null to a DateTime.

How can i construct this ?
Don't cast to DateTime - cast to DateTime? (with the question mark).
Then it should be fine.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2

should have read

row.IsRequiredByDateNull() ? null : row.RequiredByDate;

"Microsoft Newsserver" <me@nowhere.comwrote in message
news:eV**************@TK2MSFTNGP05.phx.gbl...
Hi,

Im having a bit of difficulty working out how to do the following.
method ( paramType? param1, DateTime? requiredByDate , paramType?
param3)
I have a row property which I need to pass to this method, this has a
property of IsNull which is a boolean, so I tried to do the following

method ( param1,
row.IsRequiredByDateNull() ? null, row.RequiredByDate,
param3 )

This fails on compile because it cant cast from a null to a DateTime.

How can i construct this ?

Jun 27 '08 #3
Great thanks Jon
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Microsoft Newsserver <me@nowhere.comwrote:
>Im having a bit of difficulty working out how to do the following.
method ( paramType? param1, DateTime? requiredByDate , paramType?
param3)
I have a row property which I need to pass to this method, this has a
property of IsNull which is a boolean, so I tried to do the following

method ( param1,
row.IsRequiredByDateNull() ? null,
row.RequiredByDate,
param3 )

This fails on compile because it cant cast from a null to a DateTime.

How can i construct this ?

Don't cast to DateTime - cast to DateTime? (with the question mark).
Then it should be fine.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

Jun 27 '08 #4
On Jun 23, 5:16 pm, "Microsoft Newsserver" <m...@nowhere.comwrote:
should have read

row.IsRequiredByDateNull() ? null : row.RequiredByDate;

"Microsoft Newsserver" <m...@nowhere.comwrote in message

news:eV**************@TK2MSFTNGP05.phx.gbl...
Hi,
Im having a bit of difficulty working out how to do the following.
method ( paramType? param1, DateTime? requiredByDate , paramType?
param3)
I have a row property which I need to pass to this method, this has a
property of IsNull which is a boolean, so I tried to do the following
method ( param1,
row.IsRequiredByDateNull() ? null, row.RequiredByDate,
param3 )
This fails on compile because it cant cast from a null to a DateTime.
How can i construct this ?
I once had the same problem.
I can't check it with a compiler right now, but the solution is
something like this:

row.IsRequiredByDateNull() ? (DateTime?)null : row.RequiredByDate;
Jun 27 '08 #5
yes, your absolutely correct. In fact Jon Skeet came up with the same
solution. For some reason , I never thought of casting to a nullable type.
DOH!

Thanks for your help.
"Michal Piaskowski" <pi*****@gmail.comwrote in message
news:ac**********************************@d45g2000 hsc.googlegroups.com...
On Jun 23, 5:16 pm, "Microsoft Newsserver" <m...@nowhere.comwrote:
>should have read

row.IsRequiredByDateNull() ? null : row.RequiredByDate;

"Microsoft Newsserver" <m...@nowhere.comwrote in message

news:eV**************@TK2MSFTNGP05.phx.gbl...
Hi,
Im having a bit of difficulty working out how to do the following.
method ( paramType? param1, DateTime? requiredByDate , paramType?
param3)
I have a row property which I need to pass to this method, this has a
property of IsNull which is a boolean, so I tried to do the following
method ( param1,
row.IsRequiredByDateNull() ? null,
row.RequiredByDate,
param3 )
This fails on compile because it cant cast from a null to a DateTime.
How can i construct this ?

I once had the same problem.
I can't check it with a compiler right now, but the solution is
something like this:

row.IsRequiredByDateNull() ? (DateTime?)null : row.RequiredByDate;

Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jacob Jensen | last post: by
18 posts views Thread by Marco | last post: by
1 post views Thread by Remco | last post: by
3 posts views Thread by JimM | last post: by
3 posts views Thread by g3000 | last post: by
7 posts views Thread by William S Fulton | last post: by
5 posts views Thread by brekehan | last post: by
29 posts views Thread by Tony Johansson | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.