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);
- Console.WriteLine("Haries");
- newdele.Riseevent= "Haries";
- Console.WriteLine("Vimal");
- newdele.Riseevent= "Vimal";
- Console.ReadKey();
- }
- static void gottheevent(Type o, Stringvaluechangedevente)
- {
- 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
- {
- publicDateTime changedat;
- publicstring previous;
- publicstring currentvalue;
- publicStringvaluechangedevent(DateTime changeda, string previou, stringcurrentvalu)
- {
- this.changedat = changeda;
- this.previous = previou;
- this.currentvalue = currentvalu;
- }
- }
- classdele
- {
- public delegate void namechange(Type o, Stringvaluechangedevente);
- public event namechangenamehaschanged;
- private string riseevent = "";
- public string Riseevent
- {
- get { return riseevent; }
- set
- {
- if (value!= null && value != riseevent)
- {
- Stringvaluechangedevent e = new Stringvaluechangedevent(DateTime.Now, riseevent, value.ToString());
- if (namehaschanged != null)
- {
- namehaschanged(Riseevent.GetType(),e);
- }
- }
- riseevent= value;
- }
- }
- }
- }
Details
- 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.
- newdele.namehaschanged += new dele.namechange(gottheevent);
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.
- 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.