PC Review


Reply
Thread Tools Rate Thread

can someone explain this nullable datetime error to me

 
 
Peted
Guest
Posts: n/a
 
      24th Oct 2008

Using vs2008 c#

i have this code snipet note: SUBMITTED_DT = DateTime? type

if ((DateTime.Now - billingEvent.SUBMITTED_DT).Days > 180)
{
throw new
BusinessException(Messages.BillingEvent_Save_TooOld);
}


this causes an error on build saying .Days is not supported, and
asking if i am missing an assembly reference.


if i change the code to (ie add SUBMITTED_DT.Value)

if ((DateTime.Now - billingEvent.SUBMITTED_DT.Value).Days > 180)
{
throw new
BusinessException(Messages.BillingEvent_Save_TooOld);
}

then this builds ok, i know its something to do with using DateTime?
but i dont realy understand why.

Can anyone give me a Captain Dummy explanation of why .Value is
required ?


thanks

Peter
 
Reply With Quote
 
 
 
 
Peted
Guest
Posts: n/a
 
      24th Oct 2008

I see. Ok thanks for that, that makes sense

cheers

Peter


>On Thu, 23 Oct 2008 19:54:35 -0700, <Peted> wrote:
>
>> [...]
>> then this builds ok, i know its something to do with using DateTime?
>> but i dont realy understand why.
>>
>> Can anyone give me a Captain Dummy explanation of why .Value is
>> required ?

>
>Well, I think the "Captain Dummy explanation" is simply "that's just the
>way it works".
>
>The slightly more complicated explanation is that through the magic of
>implicit conversions, subtracting a DateTime? from a DateTime results in a
>TimeSpan?, which is actually an instance of Nullable<TimeSpan>, which does
>not have a Days property (nor any other property that TimeSpan has).
>
>You have to get back to the non-nullable TimeSpan type in order to have a
>type with that property, and one of the ways you can do that is by
>short-circuiting the implicit conversions by explicitly getting the
>non-nullable DateTime Value property from your DateTime? instance. That
>removes the nullable type that was causing the expression type itself to
>be nullable, and thus the result is a plain non-nullable TimeSpan, with
>the Days property you expect.
>
>There are a variety of ways to write the code so that it works, but they
>all involve getting the expression back to the non-nullable TimeSpan
>type. For example, in addition to how you solved it, you could have
>written:
>
> if (((TimeSpan)(DateTime.Now - billingEvent.SUBMITTED_DT)).Days > 180)
>
>or
>
> if ((DateTime.Now - billingEvent.SUBMITTED_DT).Value.Days > 180)
>
>Pete


 
Reply With Quote
 
Peted
Guest
Posts: n/a
 
      27th Oct 2008

I thought that explanation was fine, actually, it does make sense to
me

thanks

Peter



>On Thu, 23 Oct 2008 22:14:43 -0700, <Peted> wrote:
>
>> I see. Ok thanks for that, that makes sense

>
>I'm glad it makes sense to you. Reading it again, I'm not sure it makes
>sense to me. The word "nullable" shows up so often, the whole thing seems
>a bit like a blur to me.
>
>But, if you're happy...I'm happy.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can someone explain this issue with nullable(of DateTime)... =?Utf-8?B?VGVycnk=?= Microsoft VB .NET 6 3rd Sep 2007 12:36 PM
Nullable DateTime variables Child Microsoft ASP .NET 5 4th May 2005 08:43 PM
DateTime not nullable problem Kevin Yu Microsoft ASP .NET 5 29th Jan 2005 10:38 AM
SqlDateTime overflow error inserting a null date into a nullable sql2000 datetime column using ado.net/vb.net, 1.1 framework Chris Microsoft ADO .NET 2 12th Sep 2004 05:02 AM
to make datetime nullable Alex Liang Microsoft ADO .NET 4 17th Dec 2003 06:23 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:21 PM.