?
Solved

statement validation

Posted on 2006-11-10
8
Medium Priority
?
239 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 400 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 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

840 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