Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method 'ToString' of DateTime? has 0 parameter(s)but is invoked with 1 argument(s)

My ProcessDate type is DateTime? when i use ToString it is showing that exception.

dfCalPlanDate.Text = concreteCalPlan.ProcessDate.ToString("d");

Thank you for your interested.

like image 568
Ilaria Avatar asked Oct 11 '25 15:10

Ilaria


1 Answers

Simple: Nullable<T> (and, thus, Nullable<DateTime>, aka DateTime?) does not have a method ToString(String).

You probably wanted to invoke DateTime.ToString(String). To do this in a null-safe way, you can use C# 6's null-conditional operator ?.:

dfCalPlanDate.Text = concreteCalPlan.ProcessDate?.ToString("d");

which is a concise way of writing:

var date = concreteCalPlan.ProcessDate;
dfCalPlanDate.Text = (date == null ? null : date.Value.ToString("d"));

Note that this will yield null if ProcessDate is null. You can append the null-coalescing operator ?? if you need another result in that case:

dfCalPlanDate.Text = concreteCalPlan.ProcessDate?.ToString("d") ?? "no date set";
like image 82
Heinzi Avatar answered Oct 14 '25 04:10

Heinzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!