473,382 Members | 1,583 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,382 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 1574
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.