1

In C# 3.0, I'm doing the code below to declare a DateTime property and a int age property that is read only.

public class MyClass{
    public DateTime dateOfBirth{ get; set; }
    public int age { get; }

    public MyClass(){}

    public int CalculateAge(){}
}

But how can I get this updated age (that it's read only) when someone enters his date of birth in a form, for example?

Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
André Miranda
  • 6,420
  • 20
  • 70
  • 94
  • 3
    I didn't even know that it was possible to declare an auto-implemented property without a set-block (private would be fine) since you can never initialize the property. – Cecil Has a Name Sep 03 '09 at 19:39
  • 3
    It's not possible to do this - the compiler will complain, precisely for the reason you give. – Pavel Minaev Sep 03 '09 at 19:40

8 Answers8

8

You need to implement the "age" property so it works off the dateOfBirth property:

public int age { 
    get {
         return (DateTime.Now - this.dateOfBirth).Days / 365;
    }
}
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
7

Don't store the age, instead calculate it when get of the age property is executed:

public int Age { get { return 100; } }

But instead of returning 100, you do the calculation.

Svante Svenson
  • 12,315
  • 4
  • 41
  • 45
2

You can't use an auto readonly property for this. You'll have to implement the property. You might also consider using a TimeSpan instead of int, since it will be more versatile.

public TimeSpan Age 
{ 
    get 
    { 
        return DateTime.Now - this.dateOfBirth; 
    } 
}
Jon B
  • 51,025
  • 31
  • 133
  • 161
2

Instead of having Age be an automatic property, implement an age calculator.

public class MyClass {
    public DateTime DateOfBirth { get; set; }
    public int Age { 
        get {
            DateTime now = DateTime.Now;
            int age = now.Year - DateOfBirth.Year;
            if(now < DateOfBirth.AddYears(age)) age--;
            return age;
        }
    }
}

You should probably refactor the above calculation out into a method, but the above demonstrates the salient point.

jason
  • 236,483
  • 35
  • 423
  • 525
1

As Svinto says, calculate the age and return that in the get method of your Age property. See How do I calculate someone's age in C#? on SO for ideas on how to do this.

Community
  • 1
  • 1
Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
1

In case you want a property which only you can assign, but which anyone can read:

public class MyClass
{
    public int Age { get; private set; }
}

Then your class can assign Age, but other classes can only read it.

Ben M
  • 22,262
  • 3
  • 67
  • 71
0

Put your code to calculate the age in your age property.

For example:

public int Age
{
     get
     {
          return (age code here);
     }
}
  • that won't compile. You can't implicitly convert a timespan to an int. – Reed Copsey Sep 03 '09 at 19:39
  • @Reed Copsey - there is no timespan in dark's code; it just says 'age code here' in the return. the actual code for computing the integer age value goes in the parantheses. – JeremyDWill Sep 03 '09 at 19:44
0

Why not change your implementation a little:

public class MyClass
{
    public DateTime DateOfBirth {get; set;}
    public int Age
    {
        get
        {
            return this.CalculateAge();
        }
    }
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536