views:

389

answers:

9

I have an output data class with a DateTime variable. I want to clear that to a null value in a loader class but the compiler complains with:

Cannot convert null to 'System.Data.Time' because it is a non-nullable value type.

I understand that, but if I change the type to DateTime? creating the nullable type wrapper I get:

No overload for method 'ToString' takes '1' arguments

I have an output line that reads.

ACCOUNT_ESTABLISHED_DATE.ToString("yyyy-MM-dd")

So the question is, when I set the DateTime as nullable, how do I get around the fact that is no longer behaves like a DateTime that has the formatted ToString available?

A: 

You would have to use

dt.HasValue ? dt.Value.ToString("...") : dt.ToString();

This is because Nullable<T> is a proper type in its own right whose ToString() method is already nicely done, as it handles the null case well. But to get to the underlying non-nullable object you have to use the Value property. But then you'll have to check for null (or HasValue) yourself.

Joey
+1  A: 

Whenever you wrap something Nullable<> (which is what you're doing with DateTime?), you need to do obj.Value.ToString().

Agent_9191
+1  A: 

try

ACCOUNT_ESTABLISHED_DATE.Value.ToString("yyyy-MM-dd")

You need to access the actual value using the 'Value' property of the nullable type.

You should make sure 'Value' contains something first testing the ACCOUNT_ESTABLISHED_DATE.HasValue property.

HTH

DaveParsons
+5  A: 

Use its Value property, like so:

DateTime? dt = DateTime.Now; // or whatever
MessageBox.Show(dt.Value.ToString(...));
MusiGenesis
Remember to account for the possibility of `dt.HasValue == false`
Joey
Its so much easier in Java. But Thanks, I did useACCOUNT_ESTABLISHED_DATE == null ? " " : ACCOUNT_ESTABLISHED_DATE.Value.ToString("yyyy-MM-dd")to clear the issue
John Putnam
A: 
DateTime? date = getSomeDate();
if (date != null) {
   date.Value.ToString("yyyy-MM-dd");
}
Fernando
A: 

are you looking for

 DateTime? dt = new DateTime();

         or 

 Nullable<DateTime> dt = new DateTime();


 ACCOUNT_ESTABLISHED_DATE.Value.ToString("yyyy-MM-dd");
anishmarokey
No. Read the question again.
Joey
+1  A: 

You should write:

ACCOUNT_ESTABLISHED_DATE.Value.ToString("yyyy-MM-dd")
empi
A: 

Have you looked at setting the DateTime to DataTime.MinValue?

Suggested here http://dotnetperls.com/datetime-null-minvalue

Brandon Haugen
+1  A: 

.NET doesn't have a method out of the box for this. You'd need to have a helper method like:

public string Format(DateTime? date, string format)
{
    if (date == null)
        return string.Empty;

    return date.Value.ToString(format);
}

Or even better, an extension method for DateTime?:

public static class DateTimeExtensionMethods
{
    public static string ToString(this DateTime? date, string format)
    {
        if (date == null)
            return string.Empty;

        return date.Value.ToString(format);
    }
}

Then to use your extension method, just use the code you have in your question and make sure the namespace of the DateTimeExtensionMethods is imported into your class.

taoufik