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 13:12:09
 
 
À
15/06/2010 11:02:53
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01468950
Message ID:
01468991
Vues:
26
>>>>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?
>>>
>>>If both are not null, cast them back
>>>
>>>
>>>  age = (int) (((DateTime)currentDate - (DateTime)birthDate).TotalDays/365.25);
>>>
>>>
>>>or
>>>
>>>
>>>int age = (int)((currentDate.Value - birthDate.Value).TotalDays / 365.25);
>>>
>>
>>Thanks Gregory, that did it.
>
>
>A nullable(T) is a structure that has a property HasValue, which is true if it is not null
>
>If it is not null, its Value member contains the Value
>
>See http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx
>http://msdn.microsoft.com/en-us/library/xkcd5cdk.aspx
>
>
>Only value types can be used for Nullable(T)

Thanks again
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform