Home
Username:
Password:
Microsoft Visual Basic .NET Tutorials

Delegates / Types of Delegates




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

In the previous two videos we've talked about delegates and what they are and how to use them and looked at some examples and code, and in those we were looking at a single delegate. At a single type delegate. As it turns out there are actually two types of delegates. Single that we've just seen and worked with, and multicast and I want to show you how multicast works. We're not going to do an example of it here, I think it will be fairly straightforward. And then I'm going to point it out to the SDK for time's sake. But let's take a look at a multicast delegate; now just like in a single, we are going to first of all create our delegate, public delegate, and this is the name of our delegate 'ExampleDelegate'. And you'll notice it takes an argument that is a single string. Now, we are going to set up a variable as a type of ExampleDelegate, we are going to setup variable A, then we are going to setup variable B as a type of ExampleDelegate, and then notice we are actually going to instantiate an object. So A now becomes an instantiated object of ExampleDelegate type and notice the address of this. A always points to the DoThis method that's somewhere else in our code. Then you'll notice we are doing the same thing with B, we instantiated an object called B of the ExampleDelegate sub up here and B always points to DoThat. So notice we are pointing to DoThis and to DoThat. Two separate methods. Now we then Dim C as ExampleDelegate. And when we instantiate C as a New Delegate and I left that line out, but notice we will then say that C = ExampleDelegate.Combine(a,b) which is right here. And so we have now got, we call C it actually invokes A and B as well. And so now we can make one delegate call to actually kick off two actions of a delegate pointer to two separate methods and cause those things to work. Now you start to see here some of the possibilities you have with events, because we can have one single event fire off a lot of different things by using multicast delegates. So that's a real quick simple run through on multicast delegates, and I would encourage you to go out to the SDK and play with these. But you can open up visual studio and do just a little bit of tinkering to figure these things out based on what you've just seen here. A lot of power in delegates, a lot of power. They are by and large hidden from us. So you just need to know about them to be able to use them. So put these in your toolbox and pull these things out as you need to carry on things in your code based on certain actions.

Tutorial Information

Course: Microsoft Visual Basic .NET
Author: Mark Long
SKU: 33433
ISBN: 1932072349
Release Date: 2003-05-27
Duration: 6 hrs / 87 lessons
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