Let's consider the following example; we will try to create a custom event for a string variable:
- Event should be raised whenever the value of string changes.
- Event should not be raised if the value is null or the same as the previous value.
- Event should provide the following information.
- Time at valued changed
- Previous value in the string
- Current value of string
- using System;
- usingSystem.Collections.Generic;
- using System.Linq;
- using System.Text;
- usingSystem.Collections.Concurrent;
- using System.Collections;
- namespace Rough_use
- {
- classProgram
- {
- public int a = 0;
- public static void Main(string[]args)
- {
- dele newdele = newdele();
- newdele.namehaschanged+= new dele.namechange(gottheevent); //subscribing to event published by "dele" class
- Console.WriteLine("Haries");
- newdele.Riseevent= "Haries";
- Console.WriteLine("Vimal");
- newdele.Riseevent= "Vimal";
- Console.ReadKey();
- }
- static void gottheevent(Type o, Stringvaluechangedevente) //this is theevent which will be raised when the value of string changes
- {
- Console.WriteLine(" Time of Changing -" +e.changedat.ToString());
- Console.WriteLine(" Current Value set -" +e.currentvalue.ToString());
- Console.WriteLine(" Previous Value Overiden -" +e.previous.ToString());
- }
- }
- public class Stringvaluechangedevent : EventArgs //this is the event details will be sent while the value of string "Riseevent" changes
- { //EventArgs - is nothing but an empty class from which all event are derived
- publicDateTime changedat;
- publicstring previous;
- publicstring currentvalue;
- publicStringvaluechangedevent(DateTime changeda, string previou, stringcurrentvalu) // setting the value to parameter that are sent in event class
- { //
- this.changedat = changeda;
- this.previous = previou;
- this.currentvalue = currentvalu;
- }
- }
- classdele
- {
- public delegate void namechange(Type o, Stringvaluechangedevente); //declaring a simple delegate
- public event namechangenamehaschanged; // creating the instant for delegate that is declared
- private string riseevent = "";
- public string Riseevent
- {
- get { return riseevent; }
- set
- { //main story starts here!!!
- if (value!= null && value != riseevent) // if the values of string is set null or the value is set to same previous value then we should not rise the event
- {
- Stringvaluechangedevent e = new Stringvaluechangedevent(DateTime.Now, riseevent, value.ToString());//initiating the value that are sent to along with event
- if (namehaschanged != null) //checking whether there are subscriber tothis event
- {
- namehaschanged(Riseevent.GetType(),e); // rising the event ..ie sent signaling to all subscriber ofthe this event
- }
- }
- riseevent= value; // finally setting the value to string
- }
- }
- }
- }
- dele is a class that wraps the string “Riseevent” that is responsible for publishing the event.
- public delegate void namechange(Typeo, Stringvaluechangedevent e);
This is the simple delegate signature. So any method that subscribes to this event should be of the same format. It should have two parameters of Type and Stringvaluechangedevent where Stringvaluechangedevent is a custom event that we have created.
- public event namechangenamehaschanged;
This is the instance of the delegate namechange.
- When the value of the string Riseevent changes we will fire the event. Hence to accompanies this task we will fire the event in the “set” property.
- if (value != null&& value != riseevent)
Here we are checking whether the value is null or the same as the previous value.
- if(namehaschanged != null)
Here we are checking whether there is any subscriber for this event.
- Stringvaluechangedevent e = new Stringvaluechangedevent(DateTime.Now, riseevent, value.ToString())
This line of code is very important. We are creating an instance of the custom class Stringvaluechangedevent. In the constructor we are ing the information that should be given to subscriber.
- So these (changedat, previous, currentvalue) will be sent to the subscriber.
- Stringvaluechangedevent is the class that is ed as the custom event to the subscriber.
Important
- public event namechangenamehaschanged;
Here if we remove the Event keyword then the subscriber will have an extra privilege to assign directly to the delegate.
Instead of the preceding format you can just use:- newdele.namehaschanged += new dele.namechange(gottheevent);
So what happens here is that any subscriber that is already subscribed to this delegate will be removed and the only “gottheevent” will be a member of the delegate.- newdele.namehaschanged = new dele.namechange(gottheevent);
- Finally the third-party class has an extra privilege over the “dele” class. So to limit his privilege I am using the event keyword.
So the subscriber can only subscribe and unsubscribe to my namechange event rather than allowing him to take control over the namechange. The delegate “event” keyword provides extra security to the delegate.

Harieswaran DPosted Dec 17, 2014, 2:35 AM
mmmm fine
Vincent SeeleyPosted Dec 16, 2014, 11:19 AM
Very nice. Thanks