Okay, so let's apply what we just learned in the last video.
Let's apply that on a temperature change handler.
So a tool that handles the change of a temperature.
And let's say once a certain temperature is hit let's say 30°C then our temperature event will be triggered.
That says, hey, the event has hit a threshold okay or exceeded a threshold.
So therefore I'm going to create a public delegate and this one will be returning void.
And I'm going to call this one temperature change handler.
All right.
And it will just print out the message.
Now obviously it doesn't need a body.
And then we're going to have a class here public class temperature monitor.
Okay.
The class has one job and it's to monitor the temperature.
So in here I'm going to have an event that will be using my temperature change handler.
And obviously we learned we should call it on temperature change.
So changed okay.
Whenever the temperature changed that's when we want to raise the event.
So that's when we want to execute the code of that event okay.
Then next I'm going to have a private int temperature and a property that will also be an int called
temperature.
And when we want to get it, it will just return underscore temperature.
And we want to when we want to set it then it will be a little more complicated okay.
So in the setter that's when we want to make a check.
So we're going to check is the temperature a certain value.
If that's the case then it starts triggering the event.
So let's set the value to the temperature.
And then we're checking if temperature is going to be let's say greater than 30.
And now you can convert that into Fahrenheit I think it's like like 95 or 90 ish or so.
And then that's where we want to raise the event.
Raise event.
Now obviously we need to prepare the event that we want to execute.
So the event that we want to execute.
So the code that we want to run will be inside of this protected virtual void race temperature changed
event, which will just take in a message and once the on temperature changed happened, then we want
to invoke it with the message.
Okay, so this is similar to what we had last time.
So now this time this is our event on temperature changed and now we are expecting this to that.
It could be zero.
That's why we're adding the question mark and then we're invoking all the listeners.
So all the subscribers, we are invoking those methods.
So we're calling those methods so to speak, and passing on the message okay.
So here when we raise the event that's where we're going to raise temperature changed event with something
like temperature is above threshold for example Okay, so now that we have that, that's so to speak,
our delegate and event that we prepared right now we need our subscriber.
So who would be our subscriber.
Let's say a temperature alert.
So public class temperature alert.
Now the could obviously be a lot of classes that subscribe to these events.
But in this case it's going to be our temperature alert.
And in here I'm just going to say public void on temperature changed.
Passing the message I want to write to the console alert colon and then whatever the message is okay.
Now let's use this in our program.
So in the main program let's create our temperature monitor.
Let's call it monitor new temperature monitor.
And then let's also create the temperature alert.
Let's call it alert new temperature alert.
And then we can add to our monitor dot on temperature changed event.
We can add our alert dot on temperature changed.
So now we can go ahead and just set the value of monitored temperature to be let's say 20 by default.
And then this will not trigger anything okay.
This will not trigger the event.
Now let's ask the user for an input.
So monitored temperature will be whatever the user.
So console dot read line whatever the user enters.
So we want to parse that into an int int parse.
And then we're going to parse whatever the user has entered.
Now obviously we could use trie and so forth.
We're going to keep it simple this time.
And let's use the let the user know please enter a temperature or the temperature.
Now obviously this temperature could come from some sensors that we have or some API that we read it
from and so forth.
Okay.
So now let's go ahead and run our little application here.
And it says please enter the temperature.
Let's enter 25.
Nothing happens okay.
So we didn't change much because the temperature didn't hit the threshold.
Let's hit 30 again.
Nothing happens now.
Let's run it again.
And this time let's say it's 31.
And then we get the alert.
It says alert temperature is above threshold.
So in this case what happened is we have our property that has some custom logic when it is set.
So now you also see another really good reason while why properties are so valuable.
So we have this private field temperature and we have this public property.
And now we say okay if you want to get it, just get it.
But if you want to set it, there are some rules.
Okay.
First of all you can set it I don't care.
But if you set it above 30 then I'm going to raise an event.
And which event am I going to raise?
Well I'm going to raise this event.
So what will happen when this event happens?
Well basically I'm just invoking all the subscribers to let them know.
Guys, the event has been raised.
Do your thing, whatever you set as logic.
And now we have this one temperature alert that sets out this alert.
But imagine it would be something like, okay, the temperature in your room becomes too hot and there
could be multiple listeners.
There could be, for example, an automated cooling system that turns on, or there could be an automated
message to your phone saying, hey, it's hot in your room, cool down your room, or something like
that, and all of these different things that trigger the events then or like, tend to trigger this
execution.
They are listeners to that event.
They're not triggering the event.
What's triggering the event is whatever information is sending the temperature to our temperature monitor.
The temperature monitor doesn't execute the code of cooling your room or sending you a message.
The temperature monitor only does one thing.
He monitors the temperature and raises an event if something specific happens.
That's it.
We could have also said something like, let's say temperature is below 20°C, which is usually the
room temperature.
It can be 2122 dependents.
So if the temperature is below 21, race another event.
And now you could say improve or increase the temperature by turning on the heating or something like
that.
For that, we would potentially create another event so that those that care about temperature being
below 20, listen to that and do their thing.
Okay.
good.
So that was number one.
Let's now have a stock price alert system as the next one.
And I think we can use that as a little bit of a challenge for you.
Okay.
So you will have to apply the same principles as we have here.
But this time with the stock price change or alert system okay.
Enjoy it.
Good luck with it and see you in the next video.