delegates

Could someone explain what a deleate is, in what occasion do we use it  and a simple example? Thank you in advance.
VBdotnet2005Asked:
Who is Participating?
 
bchoorCommented:
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()
        Debug.Print("Walk")
    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
        _Walk.Invoke()
        _Drive.Invoke(DriveSpeed)
    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:
>delegatetest.TestDelegate1
Using walk1 and drive1
Walk
Drive using old car at 10 mph
Using walk2 and drive2
Walk
Drive using new car at 60 mph

Hope this makes some sense
~BC


0
 
razorback041Commented:
a very basic example and explanation is here
http://abstractvb.com/code.asp?A=1084
and another good article here
http://www.15seconds.com/issue/020815.htm

the last time i used a delagate (yesterday) was for crossthreads...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.