473,503 Members | 9,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

comparing nullables


What is the recommended way for comparing two nullabe DateTime types using
the CompareTo method.

DateTime? d1
DateTime? d2

return d1.Value.CompareTo(d2.Value) //This obvoiusly will fail if one is null

return d1.GetDefaultOrValue().CompareTo(d2.GetDefaultOrVa lue()) //this works
but it is a hack as you are converting a null type to DateTime.MinValue and
then doing a compare.

I wish to return
-1 if d1 < d2,
0 if d1 = d2,
1 if d1 > d2

Thanks

Nov 17 '05 #1
3 2499
In many classes Compare is often implemented as a static method for null
behaviour. I see a static DateTime.Compare(t1,t2) method, but I have not
tried
it.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #2
STech,

You can always use the static Compare<T> method on the Nullable class,
like so:

DateTime? d1;
DateTime? d2;

return Nullable.Compare(d1, d2);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"STech" <st***@nospam.nospam> wrote in message
news:C7**********************************@microsof t.com...

What is the recommended way for comparing two nullabe DateTime types using
the CompareTo method.

DateTime? d1
DateTime? d2

return d1.Value.CompareTo(d2.Value) //This obvoiusly will fail if one is
null

return d1.GetDefaultOrValue().CompareTo(d2.GetDefaultOrVa lue()) //this
works
but it is a hack as you are converting a null type to DateTime.MinValue
and
then doing a compare.

I wish to return
-1 if d1 < d2,
0 if d1 = d2,
1 if d1 > d2

Thanks

Nov 17 '05 #3
Hi,
Welcome to MSDN Newsgroup!

We could write one method to compare the DateTime. At .Net 2.0, the
Nullable structure could be used for this scenario. The parameter type of
this method could be DateTime?.
//-------------------------------
static int CompareDT(DateTime? dt1, DateTime? dt2)
{
if (dt1.HasValue && dt2.HasValue)
return dt1.Value.CompareTo(dt2.Value);
else
return -2; //custom error code
}
//----------------------------------
If dt1 or dt2 is a null value, we should return error code, which assured
that there's no exception will be thrown and this snippet code will always
work.

I hope the above information is useful for you. If you have any questions
or concerns, please feel free to let me know. :)

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
3901
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
88
21947
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
2
3318
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
3
1248
by: Ricky W. Hunt | last post by:
How does VB.NET determine comparing vs. assigning? For instance, if "checkbox1.checked = True" it only checks the value but leaves it as it whereas if you have "checkbox1.checked = True" by...
19
2620
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
4
3704
by: Frank | last post by:
Hello, Developing an app where the user fills out a sometimes quite lengthy form of chkboxes, txtboxes, radbtns, etc. User responses are saved to a mySql db, which the user can later edit. When...
5
2206
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where...
20
2122
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
2
3364
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.