C# Events & Delegates Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay in this video I want to talk about events and delegates in c-sharp now this is a subject that can get kind of confusing trying to understand exactly what's going on the syntax and the methodology can get real confusing so what I'd like to do is step back and try and give some analogies of real-world stuff and hopefully help you understand what all this event stuff is and what we're trying to model and how it all works so that when you're working with these in c-sharp it makes a lot more sense and you can understand what's going on you see a lot of videos and training out there where people just go through the steps to don't really explain why you're doing it and what it all mean so I hope to help you in that aspect and show you some real-world stuff so what we'll do is we'll start out with an example let's say where we've got an office here we've got a bunch of people sitting in an office and we have a supervisor and we have an employee and let's say that the supervisor asked the employee to make four copies of a particular document so he says make me four copies of this document and he also has a procedure for the employee to follow when he's done making the copies and that is when you're done making the copies drop them on Carol's desk and this might be Carol up here in the corner sitting there drop them on Carol's desk and leave her a note telling her what they are and who they're for so the employee has to follow this procedure once he's done making the copies so let's say that the employee thinks he's got a better way to do this and let's say his response is no I'm not gonna do it that way anyone who wants me to make copies for them has to follow my rules and here are my rules you have to bring me the document first I will work on that I will make you four copies but when I am done I will announce to the entire office over the PA the public address system that I've completed the task okay I'm not going to drop them at Carol's desk I'm not gonna leave a note I'm just going to announce all the PA that I've completed the task I will also announce who I am and where to pick up the copies so I'll say this is Jo I've completed the four copies you can pick them up at the table near the water cooler okay but that's all I'm gonna do that's my procedure if you don't like it then that's your problem if you want me to work for you you must follow my rules no exceptions so that's the process he's proposing to do so the supervisor will ask for four copies Jo will do it he'll it outs to the entire office the task is finished and you got a bunch of people here and they're gonna hear it you know some people aren't going to be listening they're gonna have their their headphones in listening to music some people are gonna be talking to other people and not care about the copies but maybe out of those four copies three of them need to ultimately go to three of the employees maybe the supervisor is going to get one but he's going to distribute them to three other employees and maybe one employee is going to file a copy the other employees gonna put one in an envelope and mail it to somebody and so on but basically from the employees perspective all he will do is get on the microphone announce to the entire office the task is finished and it's up to these people to receive that message and do what they need to do that's not his problem he's just going to announce that event that the task is finished and he may tell them this is Jo and if you want to pick up the copies go to a certain desk so why is this proposed procedure good or is it good maybe it's not good well consider this let's say there are four people waiting for copies for those four copies if every one of them has their own procedures on how the copy guy should deal with them it can be very confusing for example you know if one person is going to file a copy the other person is going to put in the mail to someone the other purpose person is going to do something else and then soup is going to do something else the employee has to keep track of all those procedures and he's got to go through and it's very inefficient he's going to go through and do all those procedures and keep track and for example what if Carol leaves you know the supervisor said put it on Carol's desk what if she leaves she's not there she gets a different job maybe the employer put all four copies on her desk but Carol's not there so they'll just sit there for a week and the copiers get lost and nobody will know what happened to them and you know with these other people what if their procedures suddenly change you know he doesn't want it filed he wants it left for somebody else to do something else you have one notes those procedures change you have to go to the employee retrain him to the new procedure it could get very confusing and complicated so the idea that the employee has is that it's better to uncouple what's called uncoupling those functions and stand to have a standard set of procedures so in other words the interaction between the employee and the clients the people who are the clients of his work should be very minimal there should be very little interaction between the two each each function should be kind of uncoupled and separate and have very little reliance on the other functions okay it should be a standalone function that only passes very little information that isn't going to change so he does his thing everybody knows that they need to be listening and then they will take that information do what they want with it so it's it's a lot more efficient to have a standalone procedure that you can copy and you can use in other places not a custom procedure that it's custom for each client that he's got to do something different it's better to have in many cases a kind of a standard process that everybody follows on the other hand it requires that each of these clients follows that standard process so you know it's like anything it's not perfect but it's it's an efficient way to do things so another example of this he go in the supermarket somebody spill something in one of the aisles you hear over the the PA clean up in Aisle three that is another example of a broadcast event notification where some guy in the back room gets on the microphone and says hey an event happened in Aisle three and he notifies everybody in the supermarket the employees the customers the supervisors everybody here is that notification at least anybody who's listening so it's a broadcast event notification anybody can listen to it anybody can respond it's up to the individual whether they're gonna ignore it or they're gonna respond but basically the notifier the guy in the back room doesn't care he doesn't care who's out there he doesn't care who responds or if anybody responds his job is to just make them are not notifications so if you want to do something like that have a basically an event-driven system that is uncoupled between the the notifier and the clients who are receiving the information how would you do that in c-sharp code well I've got a diagram here basic block diagram that shows a basic concept on how you might do that and you've got the guy who's making the copies I'll call him the worker class or method in c-sharp terminology basically a method or a class that does something and when he is done with that he will make a broadcast notification an event notification that the task is complete and over here I've got a bunch of listeners basically everybody in the office or everybody in the supermarket who is listening and they are listening to that event and when that event happens when they're notified of that event they will do something based on that event you could also view them as subscribers to that event they are in their minds the people who want some of those copies are going to be listening they're going to be subscribing to that event they're going to be paying attention they're not going to be listening to music they're going to subscribe to that event and be listening for that event over the PA system once that event comes and basically it's a very simple just a hey something happened event there's not much information in there just an event occurred they will do something in their heads based on that so one person will say oh okay I'll go grab the copy and I'll file it the other person says okay I'll go grab a copy and I'll mail it to somebody and then the supervisor says oh okay I'll grab a copy and I'll keep it on my desk and so on so you basically got the the issuer of the event and the listeners are subscribers to the event that do something based on the event now in when you're gonna code this what you will generally do is you will model that broadcast as having a notification list in other words a list of subscribers who will be notified when that event is issued by this worker guy so basically the process will be something like he says okay I'm done he issues an event and there is a list of subscribers and you go down that list and once that he you received that event and you call that subscriber and say hey that that event has been issued and that task is complete so you've got a list of subscribers you call one you call the next one call the next one colleague next one and say that event has been issued the task is complete and then you can do whatever you want now in c-sharp code to get a little deeper into how you do that in c-sharp code you would have in each subscriber code like if this was a class that did something you would have an event handler method or function and basically that is just a function or a method in c-sharp that takes an event as an input parameter and there is code in here and c-sharp that does whatever you need to do it doesn't matter to the world what you do with that you have your own code but the event function has to take this event into it in order to work and each subscriber has their own version of that event hands or method or function but all of them are identical in that they take an event as an input and then they do something whatever they need to do inside that code and the list of event handlers that are called when that event happens is basically a list that says when that event happens call this function call this method and then call this method with that event okay so that's kind of you starting to see how you would do that in c-sharp and we also said that when you're talking about methods or functions in c-sharp they generally have a return value they can return a value or they can return void in this case we said it's basically a broadcast going in one direction you send out the notification this guy doesn't care to get any information back so it is a void return so each of these needs to take an event in and return nothing because this guy doesn't want anything so these all need to be standard events functions that take event and return void okay now you can take he can send out more than just a indication that his task is complete more than just a simple event he can also we said that he can say on the last speaker this is Jo the four copies are done and if you want to get those copies go to the desk or the table near the watercooler okay so he can issue not just a event no vacation but he can say okay who issued the event which was this guy and we're the copies are located all right so you can you can feed this function more information other than just the event occurred you can say who issued it and where to get that copy and then each event handler can handle that information as they need to so let's step back a bit and look at the events in the real world okay those events can be just about anything in our case in this particular example we said the event is the copies have been done the task is completed that fires off the event handler to call these methods and do what they need to do based on that but you when you're writing code you can be simulating anything you could be simulating a fire alarm which is a broadcast notification in a building that you get the lights go off and the the buzzer goes off and since there's a fire that's an event and that is you know everybody's listening hopefully everybody's a listener they hear the event and then they decide internally what they're going to do they know where what door to go out where to gather with all the people a traffic light is the same thing a traffic light is a broadcast notification the light turns red that is an event that each of the drivers in the area notices hopefully they're subscribing to that event and then they say well okay the thing is it turned red I will then stop the car if it's green then I will go if it's yellow I will slow down same thing clean up in Aisle three that's a broadcast event notification all the customers will ignore it but one guy will be subscribing and he'll go clean it up and on your computer a very common one there's an out mouse button click you click the mouse it initiates an event and tells your your methods that the button was clicked and you need to do something so it's a very very useful type of procedure to treat things as offense and then handle them based on what the event is so this list of event handler methods or functions that need to be called when the event happens how would you do that in c-sharp because really you've got to keep a list and the event happens you got to go down the list call this function call this function call this function call this function whenever the event happens how do you keep that list and how do you do those calls in c-sharp well luckily and.net there is what's called a delegate and a delegate is exactly that a delegate is a list of methods that you can call or invoke when an event happens okay now in the real world if you look at the dictionary definition of a delegate it is a person sent or authorized to represent others it is a representative someone who is authorized to resent represent somebody else now in the software world if you look at the definition of a delegate it's generally something like it's a pointer to a function or a list of pointers to functions so you scratch your head and say wait a minute how is a list of pointers to functions a representative authorized to represent somebody well this is one of the many times that software terminology leaves you scratching your head but if we we go back to our general concept on what these events what this event process is you've got the guy who's completing an event a task and he sends out an event and you've got a bunch of subscribers who are listening to that event notification and doing something this delegate is kind of the interface between the guy who is sending out the notification and all the subscribers he is basically acting as a representative to this guy who is going to then contact each of the subscribers with the notification of that event so he's kind of an interface or a representative for this guy this guy just hands off the event and leaves it to this delegate to contact the subscribers with the information about that event and to initiate those functions so you can start to see yeah it's basically a list of methods that get invoked but it's also like an interface or a representative that does the communication for this worker class or method it's kind of like a presidential press secretary almost if somebody who is authorized to represent the president and talk to the media all the subscribers and tell them about different events okay so you can kind of sort of see that then it's say the delegate is is what this thing does it's kind of an interface or a representative okay so let's go a little deeper and see how we would implement this in c-sharp here we've got the the guy that's doing the work and issuing the event notification and here we've got each subscriber so each one in order to do this you have to make sure that your your work a class or method and your subscriber class or method have implemented certain things before this process will work now the first thing that the worker the guy who's issuing the event notification doing the work first needs to define this event handler delegate okay basically the list of subscriber methods that are going to be called he has to define it alright he keeps the list of all the subscriber methods are gonna be called and he also has to define how those functions those event handler functions in the subscribers will input and output information what inputs they will take and what they will return so in our case we show that each subscriber is going to take just an event indication that he this task is complete and they're going to return nothing so this kind of defines how the subscriber functions will have input parameters and what they were returned so this guy his responsibility is to generate that event handler delegate list of methods and also define how this event handler function and each subscriber is going to be laid out and of course the next thing he has to do is he has to have a method which actually initiates the event and sends it out so he's got two things he's got to have inside his class in order for this to work now on the subscriber side of course he's got to have a method or function that handles the event alright we've already said that he's gonna have a function that takes an event and returns nothing and then inside that function or method he's got whatever code is he needs to do but the second thing he needs to do is he needs to subscribe he needs to add his name to this list of event handler functions they're going to be called when the event occurs so he has to say hey I'm out here add my name to this list so that when the event occurs you're gonna send me out an event and initiate my event handler function okay so again this guy has some requirements he's got to define the event and set up the event handler delegate he has to define how these these functions or methods will look and he has to actually have a method that initiates the event and then this guy has got to have an event handler to handle the events and do his whatever functions he wants and he also has to subscribe now when you do a subscribe just so you know dotnet makes it kind of a little bit easier when you subscribe to a event handler delegate it can automatically use a code snippet to generate an event handler in the correct format so it's a little bit easier to do that just so you know and we'll talk about that a little bit later so here's a little bit more on how you would do this in in c-sharp now so the event handler delegate we showed is kind of a separate thing in fact it is defined and resides inside this worker class method the publisher the event handler delegate is defined inside of this worker class of method so the publisher says he will call the subscribers event handler methods each of these methods but they must be in the correct format or signature and we also we already said they take an event they return nothing so it's got to be set up to do that so you can also view this interaction between the publisher and the subscriber as a type of contract that says you've got to set up your event handler method in the right way and you also have to subscribe and I have to define the event handler delegate and keep that list in order for this to work alright so you can view that as a contract that has to be set up correctly between the publisher and subscriber for this to all work now we talked about the subscriber event hams or method or function and it has to look a certain way and here is a very typical example of what that method will look like you can see it's a method it's a public void and use the name of the method and it takes two input parameters and in here you've got whatever code you need to generate so we said it's going to be public because it has to be accessed remotely by the the guy who's adding this to its to his list of event handler delegates okay so it's public it's void we said it's not going to return anything but it's going to take two types of information one is the event we said it's going to take an event it might also take in the information about who sent the event remember I said he will say okay Joe this is Joe I completed the test and I might also tell you if you want to pick up two copies you got to go to the table near the watercooler well you can feed this this event handler with not only the who sent the event but also the event itself and more information like where the copies are located so you can feed this event handler with some information where typically who sent the event and also the event itself which is this event Oryx and some other information associated with it so that kind of gives you a general idea of how hopefully how will this event stuff works in the next video we're going to apply that to a very simple application and this is going to be an image manipulation application using events and the way this is just to show you what's coming in the next video a little bit this application we're going to it's going to be a Windows forums and you click on this open image and it will open an open file dialog allowed you select - and select an image and it will taking that image and populate these two picture boxes with that image then what you do is you click manipulate image and it will modify this image and it will modify the colors of all the pixels in that image and it will then when it's done it will put that modified image in this second picture box so you can see the original and the modified when it's done doing this this modification of the colors that's going to take about three seconds there's about two million pixels in this image so it's going to take a few seconds and when it's done it's going to issue an event to the the main program that says hey I'm finished and the event handler in the main program is going to say is going to get notification of that and it's going to print in this label it's going to say the event handler received the notification so we'll go through that in the next video to show you how to implement events in an application such as this take care I hope you join me for the second video thanks
Info
Channel: EETechStuff
Views: 17,323
Rating: undefined out of 5
Keywords: C#, Events, Delegates, C# Tutorial, C# Events, C# Delegates
Id: oPD2oxoNcCM
Channel Id: undefined
Length: 26min 39sec (1599 seconds)
Published: Sun Nov 27 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.