PC Review


Reply
Thread Tools Rate Thread

Best way comparing Nullable DateTime

 
 
Alhambra Eidos Kiquenet
Guest
Posts: n/a
 
      7th Feb 2008
Hi mister,

I have an object with two properties, of type DateTime? (Nullable).

Which is the best way for comparing ? The value of datetime can be null, and
another value cannot be null, or two values are null, or two values aren't
null.

if (tarea.Fcprioridad < tarea.Fcentrega)

Can I use Comparer ??
thanks in advance. Greetings.

--
http://www.alhambra-eidos.es/web2005/index.html
www.kiquenet.net
http://www.setbb.com/putainformatica...opic.php?p=843
www.trabajobasura.com/solusoft

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      7th Feb 2008
You can use what you have... the standard behavior is discussed in
section 14.2.7 of the language spec (ECMA 334, 3rd edition); does this
not do what you need?

<q>
For the relational operators
a lifted form of an operator exists if the operand types are both
non-nullable value types and if the result type
is bool. The lifted form is constructed by adding a single ? modifier
to each operand type. The lifted
operator produces the value false if one or both operands are null.
Otherwise, the lifted operator unwraps
the operands and applies the underlying operator to produce the bool
result.
The lifted forms of the predefined operators are themselves considered
predefined operators.
</q>

I believe (from memory, I haven't checked) that
Comparer<DateTime>.Default uses similar rules; if these rules aren't
what you need, what set of results would you want? If may be you need
to check a few ".HasValue" things first to get what you want...

Marc


 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      8th Feb 2008
Manually. Here's an example. I am comparing an old and new value in a
property Set to see if the value has changed.

If both don't have a value, then nothing has changed.
If both have a value and the values don't match, it's changed.

//check to see if the value has changed; this is nullable, so it's different
//if the start date is not null and the value is null or vice versa, it has
changed
//if the start date has a value and the value has a value and they aren't
equal, it has changed
if ((_SignUpEndDate.HasValue != value.HasValue)
|| (SignUpEndDate.HasValue && value.HasValue && _SignUpEndDate.Value !=
value.Value))
{
//value is different
}

Hope that helps.

RobinS.
GoldMail, Inc.
-------------------------------------------
"Alhambra Eidos Kiquenet" <(E-Mail Removed)>
wrote in message news:C5293899-21A2-4464-9ACB-(E-Mail Removed)...
> Hi mister,
>
> I have an object with two properties, of type DateTime? (Nullable).
>
> Which is the best way for comparing ? The value of datetime can be null,
> and
> another value cannot be null, or two values are null, or two values aren't
> null.
>
> if (tarea.Fcprioridad < tarea.Fcentrega)
>
> Can I use Comparer ??
> thanks in advance. Greetings.
>
> --
> http://www.alhambra-eidos.es/web2005/index.html
> www.kiquenet.net
> http://www.setbb.com/putainformatica...opic.php?p=843
> www.trabajobasura.com/solusoft
>


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      8th Feb 2008
RobinS <(E-Mail Removed)> wrote:
> Manually. Here's an example. I am comparing an old and new value in a
> property Set to see if the value has changed.
>
> If both don't have a value, then nothing has changed.
> If both have a value and the values don't match, it's changed.


That's what you get if you do:

if (_SignUpEndDate != value)

If you look at the IL for the above expression, you'll find the
compiler has done all the work for you.



--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      9th Feb 2008

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> RobinS <(E-Mail Removed)> wrote:
>> Manually. Here's an example. I am comparing an old and new value in a
>> property Set to see if the value has changed.
>>
>> If both don't have a value, then nothing has changed.
>> If both have a value and the values don't match, it's changed.

>
> That's what you get if you do:
>
> if (_SignUpEndDate != value)
>
> If you look at the IL for the above expression, you'll find the
> compiler has done all the work for you.
>
>
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk




Okay, smarty pants. ;-) Does it compile to the same IL in VB? I'm only
asking because I got that from a VB book.

RobinS.
GoldMail, Inc.

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      9th Feb 2008
RobinS <(E-Mail Removed)> wrote:

<snip>

> Okay, smarty pants. ;-) Does it compile to the same IL in VB? I'm only
> asking because I got that from a VB book.


It may very well do different things in VB, I'm afraid - that's the
downside of the operators being implemented by the compilers rather
than the CLR/framework.

Indeed, I seem to remember that nullable equality in VB returns a
Nullable<Boolean> instead of just a Boolean.

<gratuitous plug>
For more details of the C# operator behaviour, see chapter 4 of my book

</gratuitous plug>

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      10th Feb 2008

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> RobinS <(E-Mail Removed)> wrote:
>
> <snip>
>
>> Okay, smarty pants. ;-) Does it compile to the same IL in VB? I'm only
>> asking because I got that from a VB book.

>
> It may very well do different things in VB, I'm afraid - that's the
> downside of the operators being implemented by the compilers rather
> than the CLR/framework.
>
> Indeed, I seem to remember that nullable equality in VB returns a
> Nullable<Boolean> instead of just a Boolean.
>
> <gratuitous plug>
> For more details of the C# operator behaviour, see chapter 4 of my book
>
> </gratuitous plug>
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk


<free plug>
I've already READ chapter 4 of your book. Before it was even published.
Great book, I really like how it talks about the new features of C#, and
saved me the trouble of buying another 1000 page C# book just to learn about
them. Every C# programmer should have a copy.
</free plug>

RobinS.
GoldMail, Inc.

 
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 nullable datetime error to me Peted Microsoft C# .NET 2 27th Oct 2008 12:43 AM
Problems with nullable DateTime Bill Gower Microsoft C# .NET 4 14th Feb 2007 10:31 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
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 04:26 AM.