Skip to content

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

Adam Jahn

Asked Dec 12, 2006, 4:38 PM EST1 answer
Share
LinkedInX

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

 

C#
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

C#
            {
                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::::::

 

C#
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

C#
            {
                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

Code
            {
                return schedule;
            }
            set
            {
                schedule = value;
            }
        }
}

 

 Help is appreciated.

thanks,
middly

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I figured it out...

    I did some reading on events and now it fires an event each time a variable is changed.  The nice part is it can subscribe a number of methods to the event to do things for me.  For example, i can fill a datatable with all of my employee object's data and whenever i change the object it automatically updates the datatable's info.
    0