Best way comparing Nullable DateTime

  • Thread starter Thread starter Alhambra Eidos Kiquenet
  • Start date Start date
A

Alhambra Eidos Kiquenet

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.
 
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
 
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.
 
RobinS said:
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 said:
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.

:)



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.
 
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 said:
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>

<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.
 
Back
Top