0

I have a public class PersonModel with public getters and setters. The relevant properties are:

  • DateOfBirth, a DateTime,
  • Age an int.

In the code below, I have used the shorthand curly brace shorthand notation to create an abridged version of several lines of output.<property name>.

 PersonModel output = new PersonModel
 {
    PersonId = id,
    FirstName = GetRandomItem(firstNames),
    LastName = GetRandomItem(lastNames),
    IsAlive = GetRandomItem(aliveStatuses),
    DateOfBirth = GetRandomDate(),
    AccountBalance = ((decimal)rnd.Next(1, 1000000) / 100)
 };
 output.Age = GetAgeInYears(output.DateOfBirth);

However, I would have liked to have inlined Age within the curly braces too.

However inlining this code, underneath DateOfBirth:

Age = GetAgeInYears(DateOfBirth),

leads to:

Error CS0103 The name 'DateOfBirth' does not exist in the current context..

And, I can't use:

Age = GetAgeInYears(output.DateOfBirth),

because that leads to:

Error CS0165 Use of unassigned local variable 'output'...

Is there a way to reference DateOfBirth within the curly braces when setting Age in C#4?

Something akin to the this keyword specific to curly brace initialisation (without introducing a new variable for DOB prior to constructor).

JGFMK
  • 8,425
  • 4
  • 58
  • 92

3 Answers3

1

Sorry i Did not get you correctly that's why i am putting two answers here one with constructor and other with getter and setter

you can always try something like this

public class PersonModel
{  
private DateTime dOB; 
public int PersonId {get;set;}
public string FirstName {get;set;}
public string LastName{get;set;}
public bool IsAlive {get;set;}
public DateTime DateOfBirth {get{return dOB;}set{dOB=value;}}
public decimal AccountBalance {get;set;}
public Int Age{get{return GetAgeInYears(dOB);}}
}

in initialization side it will be

 PersonModel output = new PersonModel
 {
    PersonId = id,
    FirstName = GetRandomItem(firstNames),
    LastName = GetRandomItem(lastNames),
    IsAlive = GetRandomItem(aliveStatuses),
    DateOfBirth = GetRandomDate(),
    AccountBalance = ((decimal)rnd.Next(1, 1000000) / 100)
 };

the age should be automatically calculated in class side itself

RAHUL S R
  • 1,569
  • 1
  • 11
  • 20
0

You can get the date before setting the object, save it to a variable and than initialize the object.

DateTime DOB = GetRandomDate();
PersonModel output = new PersonModel
 {
    PersonId = id,
    FirstName = GetRandomItem(firstNames),
    LastName = GetRandomItem(lastNames),
    IsAlive = GetRandomItem(aliveStatuses),
    DateOfBirth = DOB ,
    AccountBalance = ((decimal)rnd.Next(1, 1000000) / 100),
    Age = GetAgeInYears(DOB)
 };

Raziel
  • 11
  • 3
0

Change your class something like this

public class PersonModel
{
  public PersonModel()
  {
    this.DateOfBirth =GetRandomDate();
    this.Age=GetAgeInYears(this.DateOfBirth);
  } 
public int PersonId {get;set;}
public string FirstName {get;set;}
public string LastName{get;set;}
public bool IsAlive {get;set;}
public DateTime DateOfBirth {get;set;}
public decimal AccountBalance {get;set;}
public Int Age{get;}
}

now when you initialize it

PersonModel output = new PersonModel
 {
    PersonId = id,
    FirstName = GetRandomItem(firstNames),
    LastName = GetRandomItem(lastNames),
    IsAlive = GetRandomItem(aliveStatuses),
    AccountBalance = ((decimal)rnd.Next(1, 1000000) / 100), 
 };

the date and DOB will be automatically initialized from the constructor itself

now if you want to have a manual dob then create a constructor which excepts a param something like this

 public PersonModel(DateTime DOB)
      {
        this.DateOfBirth =DOB;
        this.Age=GetAgeInYears(DOB);
      } 

when you initialize it

PersonModel output = new PersonModel(DOB)
 {
    PersonId = id,
    FirstName = GetRandomItem(firstNames),
    LastName = GetRandomItem(lastNames),
    IsAlive = GetRandomItem(aliveStatuses),
    AccountBalance = ((decimal)rnd.Next(1, 1000000) / 100), 
 };
RAHUL S R
  • 1,569
  • 1
  • 11
  • 20