I have several objects with fields that will randomly be changed and updated during the execution of my program. Each object also needs to keep track of the Date/Time that the object was created and keep track of the last time any one of the fields was changed. I created a class DatedObject that has two fields: dateCreated, and dateModified. The constructor of the class sets the create date.
I then created my other objects, for example: Employee and inherited DatedObject, so that now the employee object also has the dateCreated and dateModified fields.
Now for what I hope to accomplish…
I would like for anytime I change any field in Employee, it would also update dateModified to the current time. I imagine this has a lot to do with delegates/events but I am quite unsure how to do this due to the fact the DatedObject (which contains dateModified) is inherited.
Here are my classes
:::::DatedObject.cs:::::
public class DatedObject
{
private DateTime dateCreated;
private DateTime dateModified;
public DatedObject()
this.dateCreated = DateTime.Now;
this.dateModified = DateTime.Now;
}
public DatedObject(DateTime created)
this.dateCreated = created;
/// <summary>
/// Represents the date the object was created.
/// </summary>
public DateTime DateCreated
get
return dateCreated;
set
dateCreated = value;
/// Represents the date the object was last modified.
public DateTime DateModified
return dateModified;
dateModified = value;
/// Sets the date the object was modified to the current time.
public void Modified()
:::::Employee.cs::::::
public class Employee : DatedObject
private string idnumber;
private string firstName;
private string lastName;
private Schedule schedule;
public Employee()
this.IDNumber = "";
this.FirstName = "";
this.LastName = "";
this.Schedule = new Schedule();
public Employee(string IDNumber, string FirstName, string LastName, Schedule Schedule)
this.IDNumber = IDNumber;
this.FirstName = FirstName;
this.LastName = LastName;
this.Schedule = Schedule;
/// ID Badge Number/String barcoded on employees
public string IDNumber
return idnumber;
value = value.Trim();
//Prepend Zero if doesn't meet length requirements
if (value.Length < 6)
idnumber = "000000".Substring(0,6 - value.Length) + value;
if (value.Length > 18)
idnumber = value.Substring(0, 18);
idnumber = value;
/// Employee's First Name
public string FirstName
return firstName;
firstName = value.Substring(0, 1).ToUpper() + value.Substring(1, value.Length - 1).ToLower();
/// Employee's Last Name
public string LastName
return lastName;
lastName = value.Substring(0, 1).ToUpper() + value.Substring(1, value.Length - 1).ToLower();
/// Reference to Employee's Schedule Object.
public Schedule Schedule
return schedule;
schedule = value;
Help is appreciated.thanks,middly