Thursday, October 1, 2009

Events and Delegates using C#


Delegate, Enum and Event Arguments:


public delegate void TimeSheetRejectedDelegate(TimeSheetEventArgs e);
    public enum Status
    {
        New = 0,
        Saved = 1,
        Submitted = 2,
        Approved = 3,
        Rejected = 4
    }
    public class TimeSheetEventArgs : EventArgs
    {
        long _timesheetId = 0;
        DateTime _timesheetDate = default(DateTime);
        Status _status = 0;
        string _message = string.Empty;

        public long TimeSheetId
        {
            get { return _timesheetId; }
        }

        public DateTime TimeSheetDate
        {
            get { return TimeSheetDate; }
        }

        public Status TimeSheetStatus
        {
            get { return _status; }
        }

        public string Message
        {
            get { return _message; }
        }

        public TimeSheetEventArgs(long timesheetId, DateTime timesheetDate, Status status, string MessageText)
        {
            this._timesheetId = timesheetId;
            this._timesheetDate = timesheetDate;
            this._status = status;
            this._message = MessageText;
        }
      
    }



Business Entity with Event:


public class TimeSheet
    {
        long _timesheetId = 0;
        DateTime _timesheetDate = default(DateTime);
        Status _status = 0;
       
        public event TimeSheetRejectedDelegate TimeSheetRejected;
               
        public long TimeSheetId
        {
            get { return _timesheetId; }
        }

        public DateTime TimeSheetDate
        {
            get { return TimeSheetDate; }
        }

        public Status TimeSheetStatus
        {
            get { return _status; }
        }

        public void AddTimeSheet(long timesheetId, DateTime timesheetDate, Status status)
        {
            this._timesheetId = timesheetId;
            this._timesheetDate = timesheetDate;
            this._status = status;
            if (status == Status.Rejected)
                DispatchTimeSheetRejectedEvent("Timesheet is added with Rejected Status");
        }

        public void ChangeTimeSheetStatus(long timesheetId, Status status)
        {
            if (status == Status.Rejected)
                DispatchTimeSheetRejectedEvent("Timesheet is updated with Rejected Status");
        }

        void DispatchTimeSheetRejectedEvent(string MessageText)
        {
            if (TimeSheetRejected != null)
            {
                TimeSheetRejected(new TimeSheetEventArgs(_timesheetId, _timesheetDate, _status,
                    MessageText));
            }
        }

       
    }



Invoking Point:


TimeSheet time1 = new TimeSheet();
            time1.TimeSheetRejected += new TimeSheetRejectedDelegate(time1_TimeSheetRejected);
            time1.AddTimeSheet(1, DateTime.Now,Status.New);
            time1.ChangeTimeSheetStatus(1, Status.Rejected);
            time1.AddTimeSheet(2, DateTime.Now, Status.Rejected);
            Console.ReadKey();

No comments:

Post a Comment