Posted on 2006-03-29
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.
Question by:VBdotnet2005
    LVL 5

    Expert Comment

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

    the last time i used a delagate (yesterday) was for crossthreads...
    LVL 10

    Accepted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    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.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video discusses moving either the default database or any database to a new volume.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now