4

I have filed txtb_dateOfService is required to complete the form but if txtb_dateOfService is emapty return null if not TryParse the date > I had this error I don't know how to fix it

The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments

    DateTime? dateOfService= null;
    if (string.IsNullOrEmpty(txtb_dateOfService.Text))
    {
        dateOfService = null;

    }
    else
        if (DateTime.TryParse(txtb_dateOfService.Text, out dateOfService))
        {

        }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 2
    Is there a reason you're using the nullable in the first place? This code looks like it could very easily be rewritten without nullables. – Joel Etherton Jan 03 '13 at 17:59
  • 2
    You can also use the ?? operator in C# to "dereference" a nullable (and provide a default value if the nullable is in fact null). – ravuya Jan 03 '13 at 18:01
  • What do you want to do if `txtb_dateOfService.Text` is not empty but not a valid (parseable) date/time value? – D Stanley Jan 03 '13 at 18:10

6 Answers6

6

You cannot pass a reference to DateTime? into a method expecting DateTime. You can solve this by introducing a temporary variable, like this:

else { // <<=== This is the final "else" from your code
    DateTime tmp;
    if (DateTime.TryParse(txtb_dateOfService.Text, out tmp))
    {
        dateOfService = tmp;
    } else {
        dateOfService = null;
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Your problem is converting DateTime? to DateTime, not vice versa. The DateTime.TryParse method's out parameter is not nullable; in the event TryParse fails the out parameter will be assigned DateTime.MinValue as its value. There is no reason to declare your dateOfService variable as a nullable type from this snippet.

Preston Guillot
  • 6,493
  • 3
  • 30
  • 40
1

You can either throw an exception if the parse fails:

DateTime? dateOfService= null;
if (string.IsNullOrEmpty(txtb_dateOfService.Text))
{
    dateOfService = null;
}
else
{
    // will throw an exception if the text is not parseable
    dateOfService = DateTime.Parse(txtb_dateOfService.Text);  
}

or use an intermediate DateTime to store the parsed result:

DateTime? dateOfService= null;
if (string.IsNullOrEmpty(txtb_dateOfService.Text))
{
    dateOfService = null;

}
else
{
    DateTime temp;
    if (DateTime.TryParse(txtb_dateOfService.Text, out temp))
    {
        dateOfService = temp;
    } 
    else 
    {
        dateOfService = null;
    }
}

Either of these can be logically simplified; I'm showing the full breakout to convey the logic.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

You can try to convert your string in a DateTime

DateTime? dataOfService = null;
DateTime output;

if (DateTime.TryParse(txtb_dateOfService.Text, out output))
   dataOfService = output;

now you can use dataOfService as a Nullable<DateTime> and check if it has a valid data converted using HasValue and Value properties.

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
0

you need to create a temp value to hold the out parameter of TryParse:

DateTime tmp;
if (DateTime.TryParse(txtb_dateOfService.Text, out tmp)) {
    dateOfService = tmp;
} else{
    dateOfService = null;
}

A more terse example

DateTime tmp;
DateTime? dateOfService = DateTime.TryParse(txtb_dateOfService.Text, out tmp)
   ? tmp
   : (DateTime?)null;
Charles Lambert
  • 5,042
  • 26
  • 47
0

try out dateOfService.Value, this should work (I think)

It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
  • 1
    You can't pass properties as an out parameter, see: http://stackoverflow.com/questions/1370238/passing-a-property-as-an-out-parameter-in-c-sharp – Preston Guillot Jan 03 '13 at 18:25