[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

delegates

Posted on 2006-03-29
2
Medium Priority
?
205 Views
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.
0
Comment
Question by:VBdotnet2005
2 Comments
 
LVL 5

Expert Comment

by:razorback041
ID: 16325962
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
 
LVL 10

Accepted Solution

by:
bchoor earned 2000 total points
ID: 16329061
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question