In this section, we’ll discuss more about events, Till now we have seen that how delegates work. Now, events are basically wrappers around the delegates.So, the process of defining an event with the special C# keyword event and then we are going to come and use delegate like shown below:-
public event OrderPlacedHandler orderPlaced;
so, basically as discussed earlier events on it’s own doesn’t do anything, so we need to have a pipeline to route the data from point A to point B, so that event raiser can get the data over delegates. Although, we can use delegates pretty easy way on their own like we used in earlier sections, but with events it’s also very easy way to do that
so, now let’s get started and see in real world scenario, how it actually works so for that am going to add one class in there and give it a name let’s say Implementer and this Implementer class is going to do certain works assigned to it. so, now let’s assume whoever calls this method wants to know how the work is getting progressed. so, now i’ll define a event in there. so, basically what am going to do is to grab the delegate from the main file and define the same in there in Implementer class.
Now, assume that when the work is completely done, we would like to raise an event, so in that case what am going to do is to make another event and this time am going to use built in delegate EventHandler as shown below.
so, now this Eventhandler is built in .net framework like what we have done in button_click event or any index_change event, this is there by default. so, if i just see it’s definition by doing F12 on this, then will return below delegate signature.
so, basically in this case i don’t have any custom data to pass, i just have to notify that work is done. so, this was basically defining two custom events in a implementer class. now, let’s see that how to work with these events or actually raise these events.
so, when it comes to raising an event, we just need to invoke the event just like the method. so, if you look this closely and ask these questions like what is behind the scenes of event, that is delegate. so, even we basically invoke the delegates like a method, so before we actually call an event, it’s really important to check if there is anything in the invocation list, because if delegates behind the scenes null and nothing is there, then we get exception if we try to raise the same. it’s like calling a object on a null method. so, what we generally do here, is do a quick check if the event is not null and then invoke the event like a method.
// Checking events then invoking
another option could be casting event as like delegate and see if delegate is not null and then invoke the same like shown below
orderPlacedhandler del = orderPlaces as orderPlacedhandler // done casting as delegate.
and then check if delegate is not null.
so, the proper way of raising an event to attach separate methods for each event you want to raise. so, basically my intention is i just have to get the notification @ hourly basis say like the progress of work an then when it’s completed, i should also get notified.
so, now why am doing protected virtual here, so well anybody inherits from my Implementer class, they want to override the way i raised the event, so basically am providing kind of opportunity to them to implement their own way. this is obviously my personal choice writing this way, but if you don’t want this you can certainly make it private, depend on you.
now, inside the implementation i could do certainly either of the two case we discuss above for raising the events like as shown below.
so, either way you can do this implementation. Now, what i’ll do here is I’ll make one more method for event completed like, i need to rename the earlier method to OnOrderPerformed as this was the one which gets executed on regular interval now, another method which will be raised on completed will be actually inherited from EventHandler and Eventhandler doesn’t take any parameter, hence i have stripped this off as shown below.
now, as you see in the above section it takes two generic parameter and since i don’t have any eventArgs now, so i’ll pass the same as empty like shown below:
so, now i can go ahead and raise my events as shown below
so, now whatever no of listeners we have, they will all get notified with the above methods. so, if you see this method, this method is void , but am able to return two types of data out. so, this is quite interesting. now, this works fine, but in cases where in I need to pass on a bunch of data, then in that case i would be using EventArgs.
so, now let’s see how EventArgs work in normal practice. EventArgs is quite useful when i have a lot of data to pass on and then rather passing as is like shown above would be a mess, we could use EventArgs to pass the data. EventArgs will keep the data encapsulated inside.
so, let’s get started, so what we could do here, we can write a custom EventArgs class which basically gets inherited from System.EventArgs class. and my this custom class will have all the properties which i want to pass on like shown below
so, this is very familiar POCO (Plain Old CLR Object class) like all the classes we have. so, now i have to change my delegate definition like shown below:
so, now when i have to raise the event i have to play with these two parameters declared above and when i have to handle the event i have to implement likewise. so, now it’s become very easy and clean.
so, now the actual implementation goes like as shown below, we have one Custom EventArgs defined as shown below,
Then, we go ahead and implement the same in our main Implementer class as
Let’s suppose i would like to eliminate the delegate entirely, then in that case we can say like shown below
so, basically this will generate a kind of equivalent code which used to get generated from the delegate itself. so, now in oder to use the same i need to make one small change in my code as shown below.
and that’s it now, i have a cleaner and easier way of raising an event with EventArgs. so, basically it depends on you that how you want to use the same. Most of the times when you just have events, then you can use EventHandler<T> as this just saves couple of lines of code thats it.
although, we have just wired up, but we need to call the method to do some work like shown below
ok, we have create one instance of implementer, wired up the event and then in our callback will write the hours and orderType.
so, now i would like to handle the work Completed event, so in this case it would be like
so, with this i would like to wrap this session. It seems it became quite big but anyways stay tuned and happy coding.