In this blog I will told you that how to create property notification in c#. Here I will provide you a sample through which you can easily learn that how to create property notification. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace PropertyNotificatioDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            PropertyNotification notification = new PropertyNotification();   //Create an object of PropertyNotification class.
            //Create EmployeeValueChange handler.
            PropertyNotification.EmployeeValueChange += new PropertyNotification.EmployeeNameHandler(PropertyNotification_EmployeeValueChange);
 
            //Display a message.
            Console.Write("Enter Value  :  ");
            //Read a value and initilize it in property.
            notification.EmployeeName = Console.ReadLine();
        }
 
        //Handler for property notification is created.
        static void PropertyNotification_EmployeeValueChange(object sender, EventArgs e)
        {
            Console.WriteLine("Employee name is changed."+sender);
        }
    }
 
    public class PropertyNotification
    {
        //Create a private variable which store value.
        private static string _employeeName;
        //Create a delegate of named EmployeeNamed=Handler
        public delegate void EmployeeNameHandler(object sender, EventArgs e);
 
        //Create a event variable of EmployeeNameHandler
        public static event EmployeeNameHandler EmployeeValueChange;
 
        //Create a static method named OnEmployeeNameChanged
        public static void OnEmployeeNameChanged(EventArgs e)
        {
            if (EmployeeValueChange != null)
                EmployeeValueChange(_employeeName, e);
        }
 
        //Create a property EmployeeName
        public string EmployeeName
        {
            get
            {
                return _employeeName;
            }
            set
            {
                //Check if value of property is not same.
                if (_employeeName != value)
                {
                    OnEmployeeNameChanged(new EventArgs());
                    _employeeName = value;
                }
            }
        }
    }
}

We got a notification message as well as we change value of EmployeeName property.

  Modified On Sep-18-2014 01:23:57 PM

Leave Comment