views:

9706

answers:

6

I want to use the DateTime.TryParse method to get the datetime value of a string into a Nullable. But when I try this:

DateTime? d;
bool success = DateTime.TryParse("some date text", out (DateTime)d);

the compiler tells me "'out' argument is not classified as a variable". Not sure what I need to do here. I've also tried:

out (DateTime)d.Value

and that doesn't work either. Any ideas?

+7  A: 
DateTime? d=null;
DateTime d2;
bool success = DateTime.TryParse("some date text", out d2);
if (success) d=d2;

(There might be more elegant solutions, but why don't you simply do something as above?)

Jason Kealey
You're right, I was looking for more of a one-liner to get it done, but I suppose this will do. Don't like creating that temp variable, feels messy. :-/ Seems like this scenario should be better supported.
Brian Sullivan
see Binary Worrier's suggestion to psuedo-inline that into an extension method.
David Alpert
Why are you casting a DateTime to a DateTime? You don't need to recased d2 before passing it into the TryParse.
Slace
@Slace -- I updated the answer to incorporate your suggestion.
Drew Noakes
+6  A: 

As Jason says, you can create a variable of the right type and pass that. You might want to encapsulate it in your own method:

public static DateTime? TryParse(string text)
{
    DateTime date;
    if (DateTime.TryParse(text, out date))
    {
        return date;
    }
    else
    {
        return null;
    }
}
Jon Skeet
A: 

Check out this article and this one

Cade Roux
+14  A: 

You can't because Nullable<DateTime> is a different type to DateTime. You need to write your own function to do it,

public bool TryParse(string text, out Nullable<DateTime> nDate)
{
    DateTime date;
    bool isParsed = DateTime.TryParse(text, out date);
    if (isParsed)
        nDate = new Nullable<DateTime>(date);
    else
        nDate = new Nullable<DateTime>();
    return isParsed;
}

Hope this helps :)

EDIT: Removed the (obviously) improperly tested extension method, because (as Pointed out by some bad hoor) extension methods that attempt to change the "this" parameter will not work with Value Types.

P.S. The Bad Hoor in question is an old friend :)

Binary Worrier
Ya dont wanna init the date [as you're using it as an out param]OK, I'll stop being picky!
Ruben Bartelink
Dont have compiler on me, but as DateTime is a value type, does the extension method def compile?
Ruben Bartelink
Result doesnt come back unless you make it out --[TestFixture] public class WhenExtending { [Test] public void TryParseShouldWork() { DateTime? x = null; var res = Externders.TryParse( x, "1/1/1990" ); Assert.IsTrue( res )
Ruben Bartelink
;Assert.That( x != null ); } }fails on the Assert.That, i.e., the result doesnt get modified as DateTime is a value type (which is always a nice weed-out question on phone screens :D)
Ruben Bartelink
(obv the first (non-extension) one will work, but it should be out, not ref - and you should be nulling the result if it fails to fit in with TryXXX APIs in general - Pretty sure FDG mentions that. Man, am I picky!
Ruben Bartelink
Well obviously the samples shown are for illustration purposes . . . (and Ruben STOP NIT PICKING YA BAD HOOR!)
Binary Worrier
P.S. . . . and what are these "Tests" and "Asserts" of which you speak? (Tell Me Of Your Home World Usul)
Binary Worrier
Ruben: You're right mate, an extension method won't work for this (or any other value type). Well spotted . . . cough ya bad hoor cough
Binary Worrier
+3  A: 

Here is a slightly concised edition of what Jason suggested:

DateTime? d; DateTime dt;
d = DateTime.TryParse(DateTime.Now.ToString(), out dt)? dt : (DateTime?)null;
+1  A: 

I don't see why Microsoft didn't handle this. A smart little utility method to deal with this (I had the issue with int, but replacing int with DateTime will be the same effect, could be.....

    public static bool NullableValueTryParse(string text, out int? nInt)
    {
        int value;
        if (int.TryParse(text, out value))
        {
            nInt = value;
            return true;
        }
        else
        {
            nInt = null;
            return false;
        }
    }
Strahlee