0

I have a DatePicker(CPTestDP) in our WPF window, and the date picked is saved into SQL datebase as text. I would like to use calculate the Age(how many days) based on today's date by below code. But it turns error at CFTestDP.SelectedDate.

The error message says:

A property or indexer may not be passed as an out or ref parameter.

Could anyone help please? Much appreciate.

DateTime thisDay = DateTime.Today;
DateTime startDay = DateTime.TryParse(CFTestDP.SelectedDate, out CFTestDP.SelectedDate);
TimeSpan dateAge = thisDay - startDay;
txtAge.Text = string.Format("{dd}", dateAge);
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58

1 Answers1

1

As I have seen in the documentation you should pass as second parameter in the TryParse a reference to a variable of type DateTime and it will return a boolean that tells you if the parse was ok or not. So your code should be like this:

DateTime thisDay = DateTime.Today;
DateTime startDay;
bool result = DateTime.TryParse(CFTestDP.SelectedDate, out startDay);//CFTestDP.SelectedDate should be string

if(result)
{
   TimeSpan dateAge = thisDay - startDay;
   txtAge.Text = string.Format("{dd}", dateAge);
}
else
{
   //Unable to parse
}

Source: https://msdn.microsoft.com/es-es/library/ch92fbc1(v=vs.110).aspx

SᴇM
  • 7,024
  • 3
  • 24
  • 41
Brank Victoria
  • 1,447
  • 10
  • 17