Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

statement validation

Posted on 2006-11-10
8
Medium Priority
?
237 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 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