What is the recommended way for comparing two nullabe DateTime types using
the CompareTo method.
DateTime? d1
DateTime? d2
return d1.Value.Compar eTo(d2.Value) //This obvoiusly will fail if one is null
return d1.GetDefaultOr Value().Compare To(d2.GetDefaul tOrValue()) //this works
but it is a hack as you are converting a null type to DateTime.MinVal ue and
then doing a compare.
I wish to return
-1 if d1 < d2,
0 if d1 = d2,
1 if d1 > d2
Thanks 3 2511
In many classes Compare is often implemented as a static method for null
behaviour. I see a static DateTime.Compar e(t1,t2) method, but I have not
tried
it.
Regards,
Jeff
*** Sent via Developersdex http://www.developersdex.com ***
STech,
You can always use the static Compare<T> method on the Nullable class,
like so:
DateTime? d1;
DateTime? d2;
return Nullable.Compar e(d1, d2);
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"STech" <st***@nospam.n ospam> wrote in message
news:C7******** *************** ***********@mic rosoft.com... What is the recommended way for comparing two nullabe DateTime types using the CompareTo method.
DateTime? d1 DateTime? d2
return d1.Value.Compar eTo(d2.Value) //This obvoiusly will fail if one is null
return d1.GetDefaultOr Value().Compare To(d2.GetDefaul tOrValue()) //this works but it is a hack as you are converting a null type to DateTime.MinVal ue and then doing a compare.
I wish to return -1 if d1 < d2, 0 if d1 = d2, 1 if d1 > d2
Thanks
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(DateT ime? dt1, DateTime? dt2)
{
if (dt1.HasValue && dt2.HasValue)
return dt1.Value.Compa reTo(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.) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Y contains all the
elements that are B but not in A. Z will then have the elements that are
in both A and B.
One way of doing this is of course to iterate throug the lists and compare
each of the element, but is there a more efficient way?
...
|
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 wondering if one is
better than the other or if there is some sort of hybrid of the two
that would be superior.
IMPLEMENTATION 1:
#ifndef HAVE_STRCASECMP
|
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
|
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 itself it
changes the value. Is that correct? I believe in C you have a "=" and a "=
=",
one for assigning and one for comparing. Am I understanding VB.NET
correctly?
|
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 = Color.Empty then.....
or
if mycolor is Color.Empty then .......
| |
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 the user chooses to edit, I pull the responses from the db, toss
them in a dataset, check the checks, fill the txtboxes, etc.,etc.
The user then adds, deletes, or changes entries as needed and clicks
the Save Changes button. Here is where the fun...
|
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 I'm comparing two
structs. The struct test1 has information with regards to data_size
and pointer
to address. The struct test2 has information with regards to data_size
and value. I will compare test1 and test2. For each matching data
size,...
|
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 mmapped
to a single page. I want to determine if the second
is on the page. I'd like to do:
#include "platform_appropriate_definition_of_PAGESIZE.h"
int compare1(const char *a, const char *b)
|
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 checkInteger(&$value, $checks) {
$err = '';
if (!is_numeric($value) || (floatval($value) != intval($value))) {
$err .= 'Input must be an integer. ';
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |