Creating a new event. : Delegate event : Delegate C# Examples


C# Examples » Delegate » Delegate event »

 

Creating a new event.









    
using  System;

public  class  EventTestClass  {
        private  int  nValue;

        public  delegate  void  ValueChangedEventHandler();

        public  event  ValueChangedEventHandler  Changed;

        protected  virtual  void  OnChanged()  {
                if  (Changed  !=  null)
                        Changed();
                else
                        Console.WriteLine("Event  fired.  No  handler!");

        }

        public  EventTestClass(int  nValue)  {
                SetValue(nValue);
        }
        public  void  SetValue(int  nV)  {
                if  (nValue  !=  nV)  {
                        nValue  =  nV;
                        OnChanged();
                }
        }
}

public  class  MainClass  {
        public  static  void  Main()  {
                EventTestClass  etc  =  new  EventTestClass(3);
                etc.SetValue(5);
                etc.SetValue(5);
                etc.SetValue(3);
        }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Delegate
» Delegate event