We help IT Professionals succeed at work.


VBdotnet2005 asked
Medium Priority
Last Modified: 2010-04-23
Could someone explain what a deleate is, in what occasion do we use it  and a simple example? Thank you in advance.
Watch Question

a very basic example and explanation is here
and another good article here

the last time i used a delagate (yesterday) was for crossthreads...
As razorback041 pointed out - delegates are great when using crossthreads since methods in one thread are not available to the other thread.

Just to understand the principle (I had a hard time as well), just think of a class "Activities" that has a bunch of shared methods.
Public Class Activities
    ' Class with shared methods

    Public Shared Sub Walk1()
    End Sub

    Public Shared Sub Drive1(ByVal Value As String)
        Debug.Print("Drive using old car at " & Value)
    End Sub
    Public Shared Sub Drive2(ByVal Value As String)
        Debug.Print("Drive using new car at " & Value)
    End Sub
End Class

Now, you have another class Human that will use these shared methods. So, if you look at these shared methods, you will see that there are 2 drive methods. To be able to use the 2 drive methods in your Human class you could potentially have 2 separate methods, one for Drive1 and the other for Drive2. But using delegates this could simplify your code for class Human.
Look at the code below, and you'll see that 2 delegates are dimensioned one for walk and one for drive and they both have the same signatures as the shared methods ones. Then, you expose 2 fields _Walk and _Drive that allow you to tell the class which external (external to the class) methods to invoke.

Public Class Human
    ' 2. Create delegates in the for Walk and Drive
    Public Delegate Sub Walk()      ' Make sure the signatures are the same
    Public Delegate Sub Drive(ByVal Value As String)

    Public _Walk As Walk
    Public _Drive As Drive

    Public Sub GotoGroceryShop(ByVal DriveSpeed As String)
        ' 1. To goto grocery shop, human has to walk, then drive then walk
    End Sub

End Class

Now, to finish it all - getting this to work.
First, you create a new instance of Human
Second, using the exposed fields (_Drive and _Walk) you point it to the external (external to the class) shared methods.
Third, Invoke the GotoGroceryShop method

Module DelegateTest
    Public Sub TestDelegate1()
        Dim hMan As New Human

        hMan._Drive = AddressOf Activities.Drive1
        hMan._Walk = AddressOf Activities.Walk1

        ' Try doing it the first way
        Debug.Print("Using walk1 and drive1")
        hMan.GotoGroceryShop("10 mph")

        hMan._Drive = AddressOf Activities.Drive2
        ' Now do it the second way
        Debug.Print("Using walk2 and drive2")
        hMan.GotoGroceryShop("60 mph")
    End Sub

End Module

This is the immediate window output:
Using walk1 and drive1
Drive using old car at 10 mph
Using walk2 and drive2
Drive using new car at 60 mph

Hope this makes some sense

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.