By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,071 Members | 1,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,071 IT Pros & Developers. It's quick & easy.

Best way comparing Nullable DateTime

P: n/a
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

Feb 7 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Feb 7 '08 #2

P: n/a
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" <Al*******************@discussions.microsoft.com >
wrote in message news:C5**********************************@microsof t.com...
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
Feb 8 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.