We will be undergoing scheduled maintenance on May 20th, 2013 at 02:00 GMT.
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.
Ok, here we are in visual studio and we are going to take a look at a delegate. I'm going to actually code this out for you and I apologize you are going to have to watch me type this. But as I type, just overlook the errors and the backspaces, really pay attention to how this works out, because if you can get this down then you can pretty much deal with delegates. Now, notice I'm in a console application, everything is happening a single module. And so there is our module, and then most of our action is going to take place inside this actual Sub main here. So the first thing we want to do is create a method to be called by our delegate. And I'm going to comment this so that you can go back later and freeze the screen and take a look at it if you need to. So first thing I'm going to do is create a Public Sub called PrintThis and we will pass it ByVal x as string. Now this is going to be a very simple Sub. We are only going to do a console.WriteLine and we are going to print whatever gets passed in. So very simple, but this, notice this is just a standard method, nothing special about this, nothing unusual about it. Now what we are going to do on our code is actually create our delegate, going to here define our delegate. And so notice this is going to be Public use a Delegate keyword and this is a Sub and we need to give our delegate a name. And we will call it - DelegateA - and then we will say that we will pass ByVal and we use y as string here. And notice very simple, there is our Public Delegate, this is the delegate that's the base for where we are going to instantiate our delegate from. Now in the background, when I'll compile this, in the intermediate language code it will create a class called DelegateA. So it kind of gives you a clue that these things are actually being created as classes in the background, and then we are just calling methods and we are instantiating objects and stuff. So here's the method we want to point to, here's the delegate that we are going to use to point to it. Now let's see this thing work. So in Sub main, the first thing I want to do is make the delegate that we will call. So I'm going to say - Dim delex as - and notice DelegateA; and I'm going to write this a little bit differently here just so that it's real easy for you to understand it. And now I'm going to set delex equal to the address of and now you'll notice I can put PrintThis. Now what I've told it is, I want you to create me an instance of this delegate with an address of PrintThis, which means whenever I execute this go up here and call this method. And the next thing I will do is then invoke the delegate, and I will do this by delex.Invoke and now I will tell it what I am going to pass. And I'm going to pass y as string "Hello from delegated method". Now that's all we have to do. So what's going to happen, when this program begins to run since Sub main's entry point. First thing we'll do is we would Dim delex as a variable type of DelegateA, this will be an object. Then we'll set delex equal to the address of PrintThis, and this will tell it that whenever we call delex that we want it to run, go up here and run PrintThis. Then when we invoke it, we pass the string that this requires, and our string is going to be "hello from delegated method". That goes up here into x, x comes right here and that gets printed. So let's build this, and notice our project here is called a delegate ex, delegate example. Looks like we succeeded there. So let's go to command line and run this thing, so I'm going to type delegate
| Course: | Microsoft Visual Basic .NET |
| Author: | Mark Long |
| SKU: | 33433 |
| ISBN: | 1932072349 |
| Release Date: | 2003-05-27 |
| Duration: | 6 hrs / 87 lessons |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |