473,216 Members | 1,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 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 1569
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
3
by: JimM | last post by:
I am trying to create a method in VS 2003 that validates an object argument is of the proper type and within a range of values. I am trying to use a Type to define the casting and object type for...
3
by: g3000 | last post by:
I have a problem with a Visual Studio 2005 web project. I have two pages. SelectProject.aspx and ShowProject.aspx The first page ( SelectProject.aspx) has two drop down lists. After the user...
7
by: William S Fulton | last post by:
I'm looking for the name of the following casting style in order to do some reading around on it and why it is sometimes used. unsigned long long ull = 0; void * ptr = 0; ull = *(unsigned...
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
29
by: Tony Johansson | last post by:
Hello! Here I have two different way to use casting. Is any of these any better then the other? Or is it just a question of taste. newCards.Add((Card)sourceCard.Clone());...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.