2012-03-30 20 views
11

मैं nullable दिनांक में वर्ष की गणना कैसे कर सकता हूं?वर्ष में शून्य तिथि

partial void AgeAtDiagnosis_Compute(ref int result) 
{ 
    // Set result to the desired field value 
    result = DateofDiagnosis.Year - DateofBirth.Year; 
    if (DateofBirth > DateofDiagnosis.AddYears(-result)) 
    { 
     result--; 
    } 
} 

त्रुटि है:

'System.Nullable<System.DateTime>' does not contain a definition for 'Year' and no 
extension method 'Year' accepting a first argument of 
type 'System.Nullable<System.DateTime>' could be found (are you missing a using 
directive or an assembly reference?) 
+0

यह असली दिनांक समय के साथ काम करता है? यदि ऐसा है तो आप गैर शून्य मान का उपयोग नहीं कर सकते हैं। ऐसा लगता है कि यह शून्य है साल की गणना की जरूरत नहीं है? – TGH

+0

आपको Google https://www.google.co.in/search?q=nullable+datetime+in+c%23 खोजना चाहिए था – Prakash

उत्तर

34

DateofDiagnosis.Value.Year

साथ DateofDiagnosis.Year बदलें और DateofDiagnosis.HasValue जाँच यह एक अशक्त पहली नहीं है सुनिश्चित करने के लिए।

6

पहले जांच कर लें यह एक Value:

if (date.HasValue == true) 
{ 
    //date.Value.Year; 
} 
0

आपका कोड इस तरह लग सकता है,

partial void AgeAtDiagnosis_Compute(ref int result) 
     { 
      if(DateofDiagnosis.HasValue && DateofBirth.HasValue) 
      { 
       // Set result to the desired field value 
       result = DateofDiagnosis.Value.Year - DateofBirth.Value.Year; 
       if (DateofBirth > DateofDiagnosis.Value.AddYears(-result)) 
       { 
        result--; 
       } 
      } 
     } 
संबंधित मुद्दे