Getting an Error When Comparing Date to Null?

I am getting an error when comparing date to null.  Otherwise datetime works fine.

Examples:

if (MyDate == null)
  x = 1;

Thanks,

Dan
danielolorenzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danielolorenzAuthor Commented:
The error I get is string is not instantiated.  However when the variable if populated with a real datetime, the code works fine.
0
Dave BaldwinFixer of ProblemsCommented:
From http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx ...

When you perform comparisons with nullable types, if the value of one of the nullable types is null and the other is not, all comparisons evaluate to false except for != (not equal).

The convention is that nothing is ever equal to 'null' because 'null' represents that something does not exist.  You can not make a comparison to something that does not exist.  You can only check to see if it is Not 'null'.  And 'null' is not equal to 'null' for the reason that you can not compare things that don't exist.
0
käµfm³d 👽Commented:
What is the type of MyDate?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

danielolorenzAuthor Commented:
It is of type DateTime.  Can you send me an example of how to correctly write this check?

Thanks,

Dan
0
Dave BaldwinFixer of ProblemsCommented:
Type does not matter in this case.  As it says in the Microsoft page, you can only check to see if it is Not null.  This is the case in all languages I know including SQL.

if (MyDate != null)....
0
käµfm³d 👽Commented:
@DaveBaldwin
Type does not matter in this case.
If it didn't matter, I wouldn't have asked. In C# a DateTime cannot be null. A Nullable<DateTime> or a DateTime? can be null.

@danielolorenz

How are you populating the MyDate?
0
Dave BaldwinFixer of ProblemsCommented:
That isn't the problem.  The problem is that you can not do the comparison.  You can not check to see if something equals null.  You can only check to see if it is Not null.
0
käµfm³d 👽Commented:
The problem is that you can not do the comparison.
You're correct, just not about what you think you're correct about. Your point is moot because DateTime can never be null--the compiler won't allow it. DateTime is a struct, which cannot be null. In order to make a struct accept null you have to wrap it with Nullable, which the OP has indicated he is not doing:

It is of type DateTime.

If the OP comes back and says that the type is actually Nullable<DateTime> (or DateTime?), then your point holds.
0
Dave BaldwinFixer of ProblemsCommented:
I understand what you are saying about the value.  However, in all the languages I know of, you can not ever check to see if something equals 'null'.

if (x == null) will never be a valid comparison.
0
käµfm³d 👽Commented:
if (x == null) will never be a valid comparison.
That is perfectly valid in C#. Reference-type variables can be assigned null to indicate that they are uninitialized. The following is completely legal in C#:

string s = null;

if (s == null)
{
    s = "Hello World!";
}

Open in new window

0
Dave BaldwinFixer of ProblemsCommented:
Then I don't understand the difference.  And I found out that PHP has 'NULL' as a defined value meaning no value so you can check for it.
0
käµfm³d 👽Commented:
Nullable is a special struct in .NET, and it is the only struct that can be assigned null. That is the difference. Reference types (i.e. things defined as class) can always be assigned null.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.