0

I have been using C# for trying to parse a string into datetime format. It works as long as the string is not null but fails when its null. I used datetime.parse but it failed:

Datetime.parse:

 Datetime ContactDate =   DateTime.Parse((GetContactDateForClient(1)?.DisplayText))  -- (note: getcontactdateforclient.displaytext is one of my method which gets back date in string format)

Whenever my method gets back date as non null the above works great but if its null then my above line of code fails. I tried datetime.tryparse but it shows me error during compilation ("Cannot convert System.Datetime ? to System.Datetime")
Datetime.TryParse:

        DateTime value;
        Datetime ContactDate = DateTime.TryParse((GetContactDateForClient(1)?.DisplayText), out value)  ? value: (DateTime?) null;

Is it possible to assign 'ContactDate' value as null if the string is null and if not null then get the value as it comes back(GetContactDateForClient(1)?.DisplayText). Any pointer much appreciated.

4
  • 1
    ContactDate is of type DateTime change it to type DateTime? Commented Jun 19, 2017 at 14:01
  • 5
    The exception is coming from what I'd argue is an excessively complicated one liner with a lot of conditionals. There's nothing wrong with refactoring that back into a couple of lines so that its more readable and immediately easier to debug with breakpoints. Commented Jun 19, 2017 at 14:01
  • use TryParse() instead then Commented Jun 19, 2017 at 14:02
  • He already is using TryParse? Did you not read all the question? Commented Jun 19, 2017 at 14:04

3 Answers 3

2

You need to declare a nullable datetime

try this

Datetime? ContactDate = DateTime.TryParse((GetContactDateForClient(1)?.DisplayText), out value)  ? value: (DateTime?) null;
Sign up to request clarification or add additional context in comments.

Comments

1

Is it possible to assign 'ContactDate' value as null

Sure, just make it a nullable type:

Datetime? ContactDate = DateTime.TryParse(...

That's what the original error was telling you:

"Cannot convert System.Datetime? to System.Datetime"

You were trying to assign a DateTime? (nullable DateTime) to a DateTime. They're different types. Just use the same consistent type.

1 Comment

Thanks for the response. The compile error is gone now. Let me run it end to end to see if its doing the expected in case of both null and non null string
1

You can try:

Datetime ContactDate =   DateTime.Parse((GetContactDateForClient(1)?.DisplayText ?? "your default value"))

1 Comment

Thanks for the response. I dont want to assign a default value in this case because at the end of the test , I have to compare this value with what comes back from database. So its null then , I wan to keep it null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.