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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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...
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.