Adam Jahn

Adam Jahn

  • NA
  • 3
  • 0

Help automatcally updating an inherited field variable whenever any other field is changed?

Dec 12 2006 9:38 PM

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;

            this.dateModified = DateTime.Now;

        }

 

        /// <summary>

        /// Represents the date the object was created.

        /// </summary>

        public DateTime DateCreated

        {

            get

            {

                return dateCreated;

            }

 

            set

            {

                dateCreated = value;

            }

        }

 

        /// <summary>

        /// Represents the date the object was last modified.

        /// </summary>

        public DateTime DateModified

        {

            get

            {

                return dateModified;

            }

 

            set

            {

                dateModified = value;

            }

        }

 

        /// <summary>

        /// Sets the date the object was modified to the current time.

        /// </summary>

        public void Modified()

        {

            this.dateModified = DateTime.Now;

        }

    }

 

 

 

:::::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;

        }

 

        /// <summary>

        /// ID Badge Number/String barcoded on employees

        /// </summary>

        public string IDNumber

        {

            get

            {

                return idnumber;

            }

            set

            {

                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;

            }

        }

 

        /// <summary>

        /// Employee's First Name

        /// </summary>

        public string FirstName

        {

            get

            {

                return firstName;

            }

            set

            {

                value = value.Trim();

                firstName = value.Substring(0, 1).ToUpper() + value.Substring(1, value.Length - 1).ToLower();

            }

        }

 

        /// <summary>

        /// Employee's Last Name

        /// </summary>

        public string LastName

        {

            get

            {

                return lastName;

            }

            set

            {

                value = value.Trim();

                lastName = value.Substring(0, 1).ToUpper() + value.Substring(1, value.Length - 1).ToLower();

            }

        }

 

        /// <summary>

        /// Reference to Employee's Schedule Object.

        /// </summary>

        public Schedule Schedule

        {

            get

            {

                return schedule;

            }

            set

            {

                schedule = value;

            }

        }

}

 

 Help is appreciated.

thanks,
middly


Answers (1)