Delegates / Delegates pt. 2
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
OK this is Delegates part 2 and I don't want to seem too much like those real estate programs on TLC, but let me just recap just a minute OK to help you. Actually the first thing we did is we identified methods that we wanted to call with a Delegate, in our instance we created those methods and those methods have a signature. They accept two integers as parameters and they return an integer OK? Then we went and basically created a Delegate that we're going to use to point to those methods and notice we used Public, the keyword Delegate, it returns an integer. We gave it a name, whatever name we wanted to have; Do Math and we said that it's going to accept two parameters. Now when we Click on the Button on our form let's actually use this thing. Well first of all I want to create a variable that I'm going to use that I'll have to convert OK? Now notice this is where I actually create an instance of this Delegate and you'll see where this thing kind of starts to look like a class now. I'm going to say Do Math OK and I want to create an instance of Do Math called A and I want to set it equal to a new instance of Do Math and I can point it to any of these methods that share the signature from a Delegate Object up here and in this instance I will just say Add. Now a lot just happened right there. Notice that this guy right here got treated as if he were a class because we created a new variable called A and it was a type of the Delegate Do Math and then we instantiated a new instance of it and we pointed it at our Add Method. So right here we're pointing to our Add Method. Now watch what's interesting here. I can set result which is my variable, my integer type variable right here equal to A and I pass it 3 and 4. Now what that is going to do is when this runs, A, 3, 4 is going to point to, it's going to run this particular method right here and going to return me 3 plus four and so what I will do now is put a message box up here. Oops if I hit a period that would have worked. Show and I'll have to convert that, to string result OK? Now let's run this and watch. When I click the button it will create a new instance of my Delegate called A, it'll put it to the Add Method and it will run it, drop into result and show it to us OK? So let's run this, so if I fat fingered anything, apparently not, I click that and it came back 7. Now you're thinking, OK that's kind of cool what's the big deal? Well, let's play with another one. Now this one I'm going to show, just kind of open your brain up a little, this is where we can start to layer the use of these Delegates. I am going to create right down here another method, because we can pass these Delegates to Methods as Objects. Now hang on, let me get this coded up because talk and code at the same time, just watch what I'm doing here. Now notice what I've done. I have created a method down here called Use Delegate and the parameter or argument that accepts is a Delegate. So it's going to accept my Do Math Delegate and whatever I pass it in it will run that Delegate with the numbers and I could pass numbers in with it and all kind of stuff. So let me just show you this example. So we're going to use Delegate, we're going to call, we're going to call our Method Use Delegate and we are going to pass it, the Delegate which we created which is A. So really what we're passing it is this method. We are passing this entire method as a parameter now. Make sense? And so what I will do now is give you a message box to show you the results. We're going to pass A into X and we're going to give X two parameters and A will execute, it'll point down to here and it'll bring us back, should bring us back 12 and so I will run it. We'll first get the Message Box for this one, then we'll jump down to the Use Delegate. So run this, first one we get, there's our 7 and there's our 12. So it jumped down and we actually passed that method as a parameter to this one using our delegate and the executed it inside there. I hope that makes sense. Now I know what some of you are yelling, so what good is this stuff? Learn this, get comfortable, get very comfortable with the idea of Delegates as pointers, they are actually Type Safe Function Pointers. If you did C or C++ or Java stuff you're used to Function Pointers, those things were dangerous in a lot of ways. So this is a Type Safe Function Pointer just get used to this concept and then go out and do some reading on Delegates and you can do some really cool things with them. I really hope this helps, go through this quite a few times and again look for the VTC Delegates, I'll have it all clean and pretty in your working file folder.
Tutorial Information
| Course: | Introduction to Microsoft C# 2008 |
| Author: | Mark Long |
| SKU: | 34046 |
| ISBN: | 1-935320-78-5 |
| Release Date: | 2009-10-09 |
| Duration: | 7 hrs / 76 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 