How to use Delegates and Events in C# the easy way

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey team welcome back to my channel in this video we are going to look at the c-sharp delegate and event after watching this video you will have no obstacles to use this feature in any of your programs stick around let's write a command line app that uses a delegate now employee has two fields a first name and a last name we have a method here called safe record but in this class object it is just for handling like the business rules but the first thing we have to do is we have to declare the delegate now the delegate is quite simple you just come to the top of your program and say a public delegate and this is the return value that you want to save send when they actually execute this and we're going to say save record record handler now i normally call online handler i don't know why it's not a rule i guess you can call it whatever you want to but as long as you can identify it easily and notice that i have my two fields now i call this my delegate signature this is how i know it's going to execute because when i have my event my event doesn't have any parameters on it okay let's see what that looks like so pretty much to set this up we need two things we need a delegate and then we need an event so notice the name of my event handler comes here and it's called save record handler notice it's the same and then what i always say for my event and you know this to be true it's pretty much on something if you go look in any of your html it's like on display on fresh on focus well this is another one of those it's an event if you will now watch this magic right here what is the name of this on record save so here you would say if on save i'm not sure what i just said if on save record not equal null then let's do something let's actually raise that event so notice here's my variable was saved and i say was saved equal on save record and then this dot first name this dot last name now what that does is this somebody subscribed to this and i'll show you in a moment what i mean by subscribe but someone from somewhere else has employee in their program and they are subscribing to my events and here somebody subscribed to the on safe record now let's go back to when we're doing html in html we have a thing called on blur now do you have to call on blur of course not so do they have to call on save of course not but of course if they want their record saved then they'd have to use that but other than that it's the same thing as on blur so if you understand the javascript html on blur you understand this now so here notice that i set it to false now this method on safe record you know it gets executed somewhere else it's going to give me a return value what's the return value well the return value is going to be a boolean and what that's going to do it's going to reset this command line here okay does that make sense all right cool so there is our program for employee let's see that all on one page so class employee public i got my event delegate i've got my two variables i got my constructor and then i have a method called safe record my save record has something called onsafewreck it is a event all right so let's go back to our program and now what we're going to say is we're going to say let's declare a variable of type employee so what we say we say var emp equals new employee and it had a constructor with first name and last name so let's say uh everybody remember fred sanford used to be a tv show when i was growing up and now i want to subscribe to that event they're exposing so i'm going to say emp dot on save record plus equal and notice that automatically threw me out my template code so i didn't have to program anything up and there it is right so this is the method where i could like maybe save to microsoft excel call a web service save to a dbase 3 file you know just all kinds of things but you know now it's not hardcoded up in my employees here this is just exposing the idea that a record needs to save somewhere else in your program someone saying hey i know what we're gonna do with that i'm gonna save it to a database i'm gonna call a web service i'm gonna you know just print it out to the screen oh that's a good idea why don't we just print it out to the screen notice the name of this on save record is the same name here so notice that it's going to pass in a first name and last name and i'm going to console write that and remember that return true well if we can go back to our delegate remember our delegate here on line 12 said the return value is of type bull the return value is bull and that pretty much makes sense right now here's where all the magic happens imagine you have a form and you're collecting a bunch of information first name last name now can you imagine a button on the form somewhere where then i say m.save record can you just imagine that so i noticed they're all inside of one here but i'm going to need you to think about what's happening here you can imagine a form right and then that form has got to have a save button or a cancel button well they're going to print i'm not print they're going to press the save button and then it's going to call this method called amp save record well where's emp save record emptsave record is right here but now lookup is on line 29 this event saying hey did anybody subscribe to me did someone subscribe yeah look right here on line 12. they subscribe now if i don't use this line and we're going to do two passes then this will not call this method but if it does i'm going to call this method let's step through this program okay let me move this up here so we can see what's going on all right f10 so i'm going to create fred sanford there and now i'm going to raise that event that delegates going to be saying hey if this happens i want that to happen now now watch this i can pause for the next 200 hours if i want until someone presses that save button nothing's going to happen now notice this little method over here save save record it will only execute from this other part of your program when they hit the save button let's hit f11 oh look at that it popped right in there and now it's saying hey is this null no it's not null the person actually made this assignment here on line 12. so it's saying hey okay i can actually execute this now look watch what happens here let's make sure that we don't overstep this f11 f10 and notice this is the callback method f11 and this message is saying are you sure you want to do this smart guy and then look at that i'm inside of that notice my return value is of type of bullying so i'm going to print first name last name that is first name last name and then i'm going to return true f10 return true comes back up to the save and then the save is now set to true notice i initially set it to false and now i'm going to print the output and there you have example one [Music] let us see one more example on how all this works remember i said that this is an event and it's kind of like that html event well let me just pop that in here imagine this input type equals text an on blur equals some kind of fire event well i would have to go program up fire event when this on blur executes so if i'm inside of a text box and then i try to leave it this event would get fired now that is what this line line 16 is doing this on blur and line 16 are behaving the same way so i'm going to take that out and i say hey don't execute that event don't execute on blur well what would happen when i leave a text box nothing would happen no change colors no nothing so here the whole event will not be called now i'm still gonna call safe record but look at what safe record does safe record says hey whoever's using me did he subscribe to this event yes or no if it's null if it's not null then i use this if it is null i don't do that so let's step through this program ready f10 notice i'm going to create fred sanford and now i'm up notice i did not subscribe to that event so f11 let's step in there so i'm going to initialize was save to false was safe as false now on line 29 notice on safe record is null that means it has no subscribers f10 now what's saved is false and record not saved let's look at the output of our program create a record then failure record not saved that's exactly what we want to happen here this did not execute because there was no event telling it to execute and there you have it [Music] you
Info
Channel: Software Nuggets
Views: 165
Rating: undefined out of 5
Keywords: delegates in c#, delegates and events in c#, how to consume an event, software nuggets, c# events, c# delegates, c# delegates explained, c# events and delegates, delegates c# programming guide, c# events and event handlers, c# events tutorial, c# events for dummies, c# delegates events, c# delegates for dummies, c# delegate example, c# delegates explained easy, c# events and delegates made simple, How to use Delegates and Events in C# the easy way., callback
Id: 4noBw1JFlZ0
Channel Id: undefined
Length: 11min 39sec (699 seconds)
Published: Mon Oct 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.