Solved

statement validation

Posted on 2006-11-10
8
228 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
Comment Utility
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
Comment Utility
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
Comment Utility
for example this should not give error :
DateTime? date = null;
0
 
LVL 24

Expert Comment

by:Justin_W
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 24

Expert Comment

by:Justin_W
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now