Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
System.Nullable System.TimeSpan does not contain a def
Message
De
15/06/2010 10:45:34
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
 
À
15/06/2010 10:09:47
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01468950
Message ID:
01468957
Vues:
37
Hi Frank,

How about something like this? Also, I am not sure I see the need to pass in the current date.

Update? -- Yes, what is the need to pass in a null date for? Typically there is no need to call a method if you don't have value to call if with. I added another code snippet with nullable DateTime
private void GetAge(DateTime birthDate)
{
	DateTime now = DateTime.Now;
			
	int age = DateTime.Now.Year - birthDate.Year;

	// subtract a year if before the birthdate
	if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
		age--;
}
private void GetAge(DateTime? birthDate)
{
			
     if (birthDate == null)
          return;
			
     DateTime now = DateTime.Now;
     DateTime bd = (DateTime)birthDate;
	
     int age = DateTime.Now.Year - bd.Year;

     // subtract a year if before the birthdate
     if (now.Month < bd.Month || (now.Month == bd.Month && now.Day < bd.Day))
          age--;
		}
Tim


>Hi,
>
>I am trying to write a method that will calculate the age of someone. Here is my code:
>
>
        /// <summary>
>        /// Calculates the Nearest Age
>        /// </summary>
>        /// <param name="birthDate">Date of Birth</param>
>        /// <param name="currentDate">Current Date</param>
>        /// <returns></returns>
>        public Int GetAgeNearest(DateTime? birthDate, DateTime? currentDate)
>        {
>            int age = 0;
>            if (birthDate != null && currentDate != null)
>            {
>                age = (int) ((currentDate - birthDate).TotalDays/365.25);
>            }
>            return age;
>        }
>
>And here is the error:
>
'System.Nullable<System.TimeSpan>' does not contain a definition for 'TotalDays' and no extension method 'TotalDays' accepting a first argument of type 'System.Nullable<System.TimeSpan>' could be found (are you missing a using directive or an assembly reference?)
>
>The problem I guess is that I am allowing null values to be passed in (that is what the ? does at the end of the parameter type, isn't it).
>
>How do I handle this?
Timothy Bryan
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform