Events – fire that event

An event will be fired when something has happened and then you can link to that event and do something else if you wanted to. To start of with you have to link a event with a delegate function which will be the event handler.

To setup a delegate you can have the object and also any event arguments ( you can add in more if you want to, but these are the basics to include)

	// setup a delegate for the event to fire with.
	public delegate void ChangedEventHandler(object sender, EventArgs e);

here is the class that will actual fire the event, to start with I link the “event” to a virtual method as such called Changed (with the delegate from above ChangedEventHander).

	// this is the MyEvent class that will create the event fired.
	public class MyEvent 
	{
		private int x;
 
		// the event for this internal class
		public event ChangedEventHandler Changed;

here is the event firing function, this will call the Changed “virtual” method as such when ever you call this function.

		protected virtual void ThisHasChanged(EventArgs e)
		{
			// try and call the delegate ChangedEventHandler
			// make sure that there is something to call for the event to fire.
			if (Changed != null)
				Changed(this, e);
		}

and here is the get/set for the internal X variable and in the set I call the ThisHasChanged function above.

		// call the ThisHasChanged event when you set the internal value of X.
		public int xValue
		{
			get { return x;}
			set { x = value;ThisHasChanged(EventArgs.Empty); }
		}
	}

So that is the event firing, but you will need to have a listener that will listen to any events and call something that will deal with the fired event, so here is the listener class.

So the listener, is just like any other class, but you will need to have a internal reference to the class above so that you can link to the event fired, so here is the MyEvent internal class for the listener.

	// the listener for the event firing
	class EventListener
	{
		// MyEvent class
		private MyEvent ThisIsMyEvent;

and then the constructor for the EventListener, you pass in the MyEvent class that you want to “link” (the one that you will create within the running part of the program). and from within the MyEvent, there was a “virtual” method as such called “Changed”, well now we can create a link so that when that event “Changed” has been called from within the MyEvent Class I can link to a function within this class EventListener with using a new delegate ChangedEventHandler and the ValueHadChanged is within the EventListener class.

		// the class constructor and you need to pass the MyEvent class to link to the listener.
		public EventListener(MyEvent SetUpTheEventLink)
		{
			// Create a link to the MyEvent class passed in to the internal MyEvent Class
			ThisIsMyEvent = SetUpTheEventLink;
			// this is the link to the listern function.
			ThisIsMyEvent.Changed += new ChangedEventHandler(ValueHadChange);
		}

here is the ValueHadChange, same setup as the delegate parameters since we are basically passing event details between them both and can pass in more details is you want to, but you really only want to have the object (the MyEvent class) and any event arguments e.g. errors etc. at present it just says , “Oh yeah.. I have altered”.

		// same style as the delegate e.g. object, eventargs, that is sent from the event fired.
		// THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! 
		private void ValueHadChange(object sender, EventArgs e)
		{
			Console.WriteLine("Oh yeah.. I have been altered");
		}

since there was a Changed link attached to the above function, to clean up you can detach the link with using something similar to the constructor of this class, but instead of using += (to add) you just need to subtract from the Change virtual method -=

		// detach the listener function from the event MyEvent 
		public void Detach()
		{
			// take the event linked from the MyEvent class.
			ThisIsMyEvent.Changed -= new ChangedEventHandler(ValueHadChange);
			ThisIsMyEvent = null;
		}
	}

Here is the full code

using System;
 
// when alter a number in the internal part of the class.
namespace codingfriendsEvents
{
	// setup a delegate for the event to fire with.
	public delegate void ChangedEventHandler(object sender, EventArgs e);
 
	// this is the MyEvent class that will create the event fired.
	public class MyEvent 
	{
		// the event for this internal class
		public event ChangedEventHandler Changed;
 
		private int x;
 
		protected virtual void ThisHasChanged(EventArgs e)
		{
			// try and call the delegate ChangedEventHandler
			// make sure that there is something to call for the event to fire.
			if (Changed != null)
				Changed(this, e);
		}
 
		// call the ThisHasChanged event when you set the internal value of X.
		public int xValue
		{
			get { return x;}
			set { x = value;ThisHasChanged(EventArgs.Empty); }
		}
	}
 
	// the listener for the event firing
	class EventListener
	{
		// MyEvent class
		private MyEvent ThisIsMyEvent;
 
		// same style as the delegate e.g. object, eventargs, that is sent from the event fired.
		// THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! 
		private void ValueHadChange(object sender, EventArgs e)
		{
			Console.WriteLine("Oh yeah.. I have been altered");
		}
 
		// the class constructor and you need to pass the MyEvent class to link to the listener.
		public EventListener(MyEvent SetUpTheEventLink)
		{
			// Create a link to the MyEvent class passed in to the internal MyEvent Class
			ThisIsMyEvent = SetUpTheEventLink;
			// this is the link to the listern function.
			ThisIsMyEvent.Changed += new ChangedEventHandler(ValueHadChange);
		}
 
		// detach the listener function from the event MyEvent 
		public void Detach()
		{
			// take the event linked from the MyEvent class.
			ThisIsMyEvent.Changed -= new ChangedEventHandler(ValueHadChange);
			ThisIsMyEvent = null;
		}
	}
 
	class TestMyEvent
	{
		public static void Main()
		{
			// create a new MyEvent class
			MyEvent MyEventToCheck = new MyEvent();
 
			// link in with the EventLister , passing in the class of MyEvent
			EventListener TheListener = new EventListener(MyEventToCheck);
 
			// should fire of a event !! because I am changing the internal x value.
			MyEventToCheck.xValue = 10;
 
			// will not fire a event because I have not changed it.
			Console.WriteLine("X = " + MyEventToCheck.xValue);
 
			// will fire of another event.
			MyEventToCheck.xValue = 5;
 
			TheListener.Detach();
		}
	}
}

and here would be the output

Oh yeah.. I have been altered
X = 10
Oh yeah.. I have been altered

If you want to gain access to the object sender within the EventListener class to the function that was listening to the event fired, if you alter it as below, but also since because the MyEvent was also linked within the constructor you also have that link as well.

		// same style as the delegate e.g. object, eventargs, that is sent from the event fired.
		// THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! 
		private void ValueHadChange(object sender, EventArgs e)
		{
			Console.WriteLine("Oh yeah.. I have been altered");
			Console.WriteLine("New value of X = " + ((MyEvent)sender).xValue);
			Console.WriteLine("Internal MyEvent class x = " + ThisIsMyEvent.xValue);
		}

then you can gain access to the MyEvent from within the object sender and the output would be

Oh yeah.. I have been altered
New value of X = 10
Internal MyEvent class x = 10
X = 10
Oh yeah.. I have been altered
New value of X = 5
Internal MyEvent class x = 5

Leave a Reply

Your email address will not be published.