Solved

statement validation

Posted on 2006-11-10
8
234 Views
Last Modified: 2010-04-16
Why isn't this statement valid ?
DateTime? st = this.SunSt.Text != "  :" ? DateTime.Parse(this.SunSt.Text) : null;

I get an error that
Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and '<null>'

Thanks,
0
Comment
Question by:psokolo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 24

Expert Comment

by:Justin_W
ID: 17916082
DateTime is a ValueType and a DateTime variable can never have a value of null.

Try something like this:
    DateTime d = (this.SunSt.Text != "  :" ? DateTime.Parse(this.SunSt.Text) : DateTime.MinValue);
or this:
    object obj = (this.SunSt.Text != "  :" ? DateTime.Parse(this.SunSt.Text) : null);
    if (obj != null) { /* do something */ }
0
 

Author Comment

by:psokolo
ID: 17916110
I thought that DateTime? data type is nullable (I am using NET 2.0) isn't it ?

I am trying to pass value to data access method.
0
 

Author Comment

by:psokolo
ID: 17916147
for example this should not give error :
DateTime? date = null;
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 24

Expert Comment

by:Justin_W
ID: 17916169
psokolo,
> I thought that DateTime? data type is nullable (I am using NET 2.0) isn't it ?
No, it's not (see http://msdn2.microsoft.com/en-us/library/system.datetime.aspx). If you need to be able to assign a null value to your variable, you will have to use a non-struct/ValueType type (e.g. an object variable).
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17916181
> for example this should not give error :
> DateTime? date = null;

It doesn't give an error, but after the statement, the variable still isn't null. It contains the "Default" value for the type, which happens to be DateTime.MinValue.
0
 

Author Comment

by:psokolo
ID: 17916215
I work with Typed Datesets. If I pass DateTime.MinValue to Insert method in TableAdapter it should insert null value to DB ?

Good to know that the values are not actually null.
0
 
LVL 24

Assisted Solution

by:Justin_W
Justin_W earned 100 total points
ID: 17916428
Yes, Typed DataSets do a lot of work for you, and this could include converting DateTime.MinValue to DBNull.Value in some cases.
0
 
LVL 6

Accepted Solution

by:
VovinE earned 150 total points
ID: 17916542
The compiler has answered you why it cannot compile.

when you use inline conditional statement (using ? character) then both sides (true and false) has to be of the same (or castable) type.

so you could correct your statement like this:

DateTime? st = this.SunSt.Text != "  :" ? DateTime.Parse(this.SunSt.Text) : (DateTime?)null;

and the compiler knows, that it should treat boh return values as DateTime?.

When you compare nullable type against null value then even the nullable type isn't really null it can evaluate true, so:

DateTime? dt = null;
if(dt == null)
{
   do something.
}

will execute "do something" block. But it is much better to check HasValue for null checking.


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question