Solved

statement validation

Posted on 2006-11-10
8
233 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

734 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