Question

I have a number of objects in my project which have fields that will contain matching values when they're initialized - see the code snippet below for an example, in particular the AddedDateTime and ModifiedDateTime properties:

User user = new User()
{
    FirstName = "Tex",
    Surname = "Murphy",
    AddedDateTime = DateTime.Now,
    ModifiedDateTime = DateTime.Now
};

Is it possible to set the value of ModifiedDateTime to the value of AddedDateTime, within the {...} block once it has been set?

I have a feeling it's not possible but thought it worth asking for confirmation.

Was it helpful?

Solution

I'm not sure if this what your after or not, but is this appropriate?

var now = DateTime.Now;
User user = new User
{
     FirstName = "Tex",
     Surname = "Murphy",
     AddedDateTime = now,
     ModifiedDateTime = now
};

If you want to have them to initialize to the same value then you must set to a common variable value.

OTHER TIPS

You could maybe put this logic into the implementation of the properties:

public class User{
  private DateTime added;
  private DateTime? modified;

  string FirstName {get;set;}
  string SurName {get;set;}

  DateTime AddedDateTime { 
   get { return added; } 
   set { added = value;
         modified = modified ?? value;
       }
   }

  DateTime? ModifiedDateTime {
    get { return modified }
    set { modified = value; }
  }
}
User user = new User
            {
                FirstName = "Tex",
                Surname = "Murphy",
                AddedDateTime = DateTime.Now < Something? DateTime.Now : SomeOtherValue,
                ModifiedDateTime = DateTime.Now < Something? DateTime.Now : SomeOtherValue
            };

As mentioned in a comment, you could make constructors that contain just the data that you need, i.e.:

public User(string firstName, string sirname)
{
    FirstName = firstName,
    Surname = sirname,
    AddedDateTime = DateTime.Now,
    ModifiedDateTime = DateTime.Now
};

You could also have helper methods to create instances of the class. They could be inside of the class or not (this is useful if you can't control the class itself)

public static User CreateUser(string firstName, string sirname)
{
    User newUser = new User();
    newUser.FirstName = firstName,
    newUser.Surname = sirname,
    newUser.AddedDateTime = DateTime.Now,
    newUser.ModifiedDateTime = DateTime.Now

    return newUser;
};

You could also have something like:

public static User CreateUser(string firstName, string sirname, string otherdata)
{
    User newUser = new User();
    newUser.FirstName = firstName,
    newUser.Surname = sirname,
    newUser.AddedDateTime = DateTime.Now,
    newUser.ModifiedDateTime = DateTime.Now,
    newUser.SomeField = otherdata,
    newUser.SomeOtherField = otherdata

    return newUser;
};

You could put logic into the setter of your AddedDateTime that checked for a minimum value on the ModifiedDateTime and set accordingly. e.g.

public DateTime ModifiedDateTime { get; set; }
private DateTime _addedDateTime;
public DateTime AddedDateTime {
    get { return _addedDateTime; }
    set 
    {
        if (ModifiedDateTime == DateTime.MinValue)
        {
            ModifiedDateTime = _addedDateTime = value;
        };
    }
}

...

var test = new Test 
{ 
   AddedDateTime = DateTime.Now
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top