Welcome back.
In this video we're going to use the generic delegate event handler t event args.
So a lot of words here right.
So we know what a delegate is.
And we don't really know yet what an event handler is.
And neither what t event args are.
We have heard the word generic before, but let's look and into why this would matter.
So first of all why do event args matter and what are event args?
Event arcs are event arguments.
So basically when an event is raised, you want to sometimes pass information about the event.
So first of all, who was the person or the the object that raised the event.
And secondly, some additional information, some additional arguments that you want to pass on to whoever
is now consuming the event.
So to the subscribers, because the subscribers, maybe they need this information that is information
relevant to that event and they want to display it, or they want to work with it later on or something
like that.
And in order to achieve that, you need to use event arguments.
And usually what you will see is the following.
So there's this generic delegate called event handler.
Let's create a public event and this one will be the event handler.
This one here.
Actually it's this one.
This is the generic one.
So if you hover over it you will see that it says event handler t event args object sender t event args
okay.
So let's look at this one.
So what we can do in this event handler we can check out what it says.
So this event handler is a delegate right.
And we know that we need delegates in order to prepare events.
Now this event handler also has two arguments.
One of them is the sender which is of type object nullable.
And the other one is event args.
So the event args is just the base class for classes that contain event data, and provides a value
to use for events that do not include event data.
So if you want to create your own event that then also sends data to the subscribers.
You need to use event args.
Now the cool thing is you can obviously inherit from event args, because if we look at event args you
can see it's a public class.
It's not sealed so we can inherit from it.
And there are many event args subclasses.
So inheriting classes of the Eventargs class.
It's very simple.
As you can see, the event args class.
The only thing it does, it has the static read only event args, which is an empty event args object.
And it doesn't do much right, but it becomes very powerful once we use it for our own workings.
Okay, so in order to do that, let's start by following the following thing.
So let's create our own custom Eventargs class.
All right I'm going to create it up here.
So I'm going to say public class temperature changed event args.
So this event args name here is important.
So that we know this class is inheriting from event args.
It's just common practice.
Okay, so now our own class is inheriting from this event args class and is extending it, which means
it's adding functionality to it.
Right.
So the information that I want to store is going to be the temperature.
And this one I'm just going to be able to get it.
I'm just going to say don't set it.
Okay.
So this temperature event arcs.
And if I want to set the temperature I can only do that in the constructor.
So public temperature changed event arcs which is the constructor.
And I'm passing the temperature.
And I'm storing that temperature in the object of the event arcs.
So the event arcs now will hold the temperature okay.
So whenever we create an object of it.
So this is the constructor and this is the temperature property.
All right.
So now that we have these event arcs the next step would be to use them inside of our temperature monitor.
Because I want to whenever I raise the event I don't want to pass in just a string message.
I actually want to pass in the temperature event arcs, whatever those are.
So I can now say.
Temperature change, event arcs.
And a common name is E okay.
So usually when you're using event arcs you're just calling it E.
So now when I'm changing the temperature I'm going to pass E.
Now this won't work because our method here our on temperature changed event.
It follows this temperature change handler which expects a message.
It doesn't expect a event args data type.
So now we're going to modify this whole thing.
We're not going to use this delegate anymore which is why we can't use this line anymore.
Instead we're going to create a generic event handler event.
So this one here with the temperature changed event args.
And we're going to call it temperature changed.
So now this event handler has to be an event obviously.
So we need to add the event keyword.
And if you look at it the event handler is just a delegate right.
So it's basically the same thing as when we created this event right here, which also followed the
temperature change handler which was a delegate.
Okay, so now it's just that event handler is a predefined delegate that now is going to accept temperature
change event arcs.
So you see here it's of type generic event arcs.
So what this means this generic event arcs we've seen generic in general, but generic event arcs just
says any type of event arcs.
I don't care if it's the base class, event arcs or anything that you made out of event arcs by inheriting
from it, which is what we did here.
Okay, we inherited from event Arcs, and now we can use temperature changed event arcs for our event
handler.
Okay.
And on top of that, what you will see is it says give me a sender and give me the event arcs.
So whenever we now want to use our event handler that we use here, now the method that subscribes to
the temperature changed event needs to pass the sender object as well as the temperature change event
arcs, which now means we need to go down to our subscriber.
So this here is our subscriber.
And this temperature alert has this on temperature changed method which now needs to accept the object
sender as well as the temperature change event arcs E.
And now in here we can now say alert.
And let me actually use string manipulation here.
Alert and temperature is and then E dot and E holds the temperature.
So now this event arguments object holds information about the temperature.
Because if we look at it if we look at this temperature changed event arcs that we created, it has
this property called temperature.
Now let's extend that and also say who is the sender.
So sender is and then sender.
And I'm just going to say sender okay.
So this sender right here okay.
Now that this has changed we need to make a change to this part here as well okay.
So now raise temperature changed event I'm going to call this one on temperature changed and what it
will do is it will use our temperature changed event and it will invoke it with this and E.
So what is going on in this line?
This is a crazy cool line because what we're saying is there is this event called temperature changed.
And to any if there are any ones, any subscriber I want to invoke it and I want to invoke you.
I want to basically call this method for every single subscriber.
So this is just one subscriber or temperature alert, but there could be other subscribers as well to
every single one of them.
I say, hey, I'm going to invoke you.
I'm going to call your method on temperature change now, because in our later point you can see here
we are adding it.
So here let's change that to temperature changed because that's the new if you look at it, that's the
name of our event handler here.
And then and at this line we're basically using this temperature alert.
If you recall this is the temperature alert object.
And in this line what we're saying is okay the temperature changed event now has a new listener.
And the listener is or subscriber.
Is this on temperature changed method and once the temperature changed event will be triggered.
Call this method.
So call this on temperature changed method.
When will it be triggered.
Well that's the logic we have in here inside of our temperature monitor.
That raises the event right.
So that's how we raise the event earlier.
Now we want to call this method in the right moment.
So what I'm going to do is I'm going to modify the code a little bit.
I'm going to say we're going to raise the event each time that the temperature is a new temperature.
So each time that the temperature value is not equal, the value that is passed to set the temperature.
So where are we setting the temperature here.
This is where we're setting the temperature of our temperature monitor.
So this is the temperature monitor class right.
So each time we're setting the temperature for example we're reading it from the API or we're reading
it from a sensor.
We're checking is the temperature a new temperature.
And if it is then let's set the new temperature.
And on top of that let's also raise the event.
So we're going to call our own temperature changed method.
And we're going to pass the event.
How can we now pass the temperature change event arcs E.
Well we can create a new temperature, changed event arcs.
And we can pass the temperature well.
We can either pass the value or we can also pass the underscore temperature.
At this point it's the same thing right.
Because we overrode the temperature with the value.
So what we're now saying is, and this is now everything coming together, whenever I'm raising the
event, I'm giving you some event ARGs in this case just one, which is the temperature.
So then the actual call of the method.
So then this here right away is just calling this method.
Right.
But then but in here that's where we are invoking the event and letting everybody know every subscriber.
So here we're saying letting every subscriber know.
And what do we let them know?
We let them know about the event arcs, which is in this case just the temperature.
But it could be a lot more.
It could be so much more information.
It's full like in your full control now.
And I'm also passing the object that invoked the event.
So the object that raised the event.
In which object is it?
It's this.
What is this?
This is the events app dot temperature monitor class.
So if we scroll out we see it's actually this temperature monitor class.
Because this this keyword is used in the context of this class.
So it's the class.
So the object temperature monitor that we are passing.
It's not the class itself but it's the object.
Because if we look at it here when we are setting the temperature, we're doing it in the monitor.
Who is the monitor.
It's the temperature monitor class.
So it's this class right here.
And this this keyword is inside of the class, so it will just pass the object.
Let's just test it and you will see that this is true okay.
So let me show you.
And there we are alert temperature is 20 Sanders events app temperature monitor.
So you see now our alert goes on every single time the temperature is changing.
So if we change the temperature to let's say 23 degrees then it says temperature is 23 centrist events
at ten temperature monitor.
So the sender which is this sender object for our subscriber, whoever the subscriber is it doesn't
matter.
It could be like a third or fifth subscriber.
We could have ten subscribers.
Let's create another subscriber just quickly copying this.
So this one will be let's say, uh, temp, um, cooling alert.
All right.
And it's going to do the same thing, and it's just going to say temp cooling alert.
All right.
And now let's go ahead and add that temp cooling alert as well.
So temp cooling alert alert two new temp cooling alert.
And now our monitor is going to also subscribe or like our alert two, which is our temp cooling alert
is also going to subscribe to the Temperature Change monitor event.
So now if we run this it will trigger for both of them.
So you see the sender in both cases is events App Temperature monitor.
Because that's the class name of the object that is raising the event okay.
So at this point I think we've gone through the entire thing.
Now, you know how you can pass your own information, how you can create your own event arcs, so to
speak, to pass your own details like temperature or whatever other parameters you want to have.
You've seen how to use the generic event handler, and you've seen how to raise events as well as how
to have subscribers.
So this is subscriber two.
And then how to bring everything together to actually execute it okay.
So that's it for this video.
The longest video in the course so far.
But I just couldn't put it into individual videos.
Um, as each individual part really didn't make sense without the other parts.
It's really like everything belongs together.
But now you got it.
Congratulations.