I’m a bit unsure on how I’d word this question, so pardon me if this is duplicate.
Basically I want to call UpdateModifiedTimestamp everytime a property is changed. This is just a sample class I wrote up pretty quickly, but should explain what I’m trying to achieve.
Everytime Firstname, Lastname, or Phone is changed it should update the ModifiedOn property.
public class Student {
public DateTime ModifiedOn { get; private set; }
public readonly DateTime CreatedOn;
public string Firstname { set; get; }
public string Lastname { set; get; }
public string Phone { set; get; }
public Student() {
this.CreatedOn = DateTime.Now();
}
private void UpdateModifiedTimestamp() {
this.ModifiedOn = DateTime.Now();
}
}
What you are describing sounds pretty close to the property change notification usually done via the
INotifyPropertyChangedinterface. Implementing this interface would give you a little more generic solution to your problem:This approach would make the change notification available to consumers of your class as well, if that’s what you are shooting for, a different solution probably would be preferable.