Casting null values

  • Thread starter Microsoft Newsserver
  • Start date
M

Microsoft Newsserver

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 ?
 
J

Jon Skeet [C# MVP]

Microsoft Newsserver said:
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.
 
M

Microsoft Newsserver

should have read

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

Micha³ Piaskowski

should have read

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

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top