Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No overload method for ToString takes 2 arguments

Tags:

c#

.net

I am trying to format a datetime to a string of the form "c", by using the ToString method:

string end = evt.End.DateTime.ToString("c", DataController.culture);

DataController.culture is a reference to a valid culture object and evt is an Event object from the Google Calendar v3 API. I am sure DateTime is of the DatetTime class because the debugger says so in the type of the object.

However I keep getting the issue mentioned in the title, why? Even the MSDN docs show how to use that method but somehow I can't:

Console.WriteLine(date1.ToString("d MMMM", 
                  CultureInfo.CreateSpecificCulture("es-MX")));
// Displays 29 agosto    
like image 667
arielnmz Avatar asked May 16 '16 02:05

arielnmz


1 Answers

The API documentation indicates that the property you're attempting to ToString() is actually a System.Nullable<DateTime>, which has different usability considerations from a normal DateTime. In order to invoke the ToString method you wish, you need to retrieve a concrete DateTime struct from the nullable one provided to you - in your case, by doing the following:

string end = evt.End.DateTime.Value.ToString("c", DataController.culture);

Mind you, you normally will want to first check that your nullable object (eg. evt.End.DateTime) actually has a value by checking the HasValue property before invoking ToString() or any other method or property.

like image 96
Mike G Avatar answered Oct 13 '22 01:10

Mike G