borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (CoolBar related question ?)Next Topic (Display pictures  on Forms in VB.NET) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : what is delegate and what is their use.
Poster Message
tri_inn
Level: Regular User
Registered: 26-08-2002
Posts: 395

icon what is delegate and what is their use.

please tell about delegate and show me their easy use if possible.

05-08-2004 at 06:21 AM
View Profile Send Email to User Show All Posts | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon Re: what is delegate and what is their use.

In .NET, you are using delegates almost all the time when you respond to events. A delegate is simply a piece of code that will execute on behalf of another. If you ever used Pascal, you would know that you could have function variables where you could define a variable as a function. The actual function implementation would/could be written somewhere else, and by someone else, and then assigned to this variable. The original code would call the variable and any function assigned to it would be executed.

A delegate is a similar thing, you can define your own for many reasons, and you decide on the signature that you want for the delegate. You basically do it this way

Public Delegate Sub DoSomething()


Classes that implement your delegate can call it what they will as long as the signature is the same, i.e. - no return type and no arguments. (Don't talk back and don't argue). I use delegates to execute certain code asychronously, such as building my user interface while loading info from  the database:

Dim lb As New LoadBoothsDelegate(AddressOf LoadBooths)
Dim aresult As IAsyncResult = BeginInvoke(lb)


The BeginInvoke allows a method to be executed on the same thread as the calling routine but allows the rest of normal processing to go on without waiting for the code to be finished first. Delegates are useful in this area. Some people actually use them in place of events but that is another story altogether. Happy coding

____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

07-08-2004 at 10:57 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
johnedw
Level: Sage

Registered: 15-08-2004
Posts: 50
icon Re: what is delegate and what is their use.

fabulous, man you are FABULOUS!

I din't really know much about BeginInvoke, very useful.


but this might be helpful as well..

This is an example of how to find out what methods are attatched to a delegate associated with an event.

Public Class Job
    Public Delegate Sub MessageX(ByVal Message As String)
    Public Shared Event Alert As MessageX

    Public Shared Sub Report()
        Dim X As Integer
        Dim Test() As [Delegate]
        Test = AlertEvent.GetInvocationList()
        Console.WriteLine("Delegate Subscription Information")
        For X = 0 To Test.Length - 1
            Console.WriteLine("Delegate Subscribed: " & Test(X).Method.Name)
        Next
    End Sub
End Class

____________________________
Today is a new day, in
a world OF CODE!

15-08-2004 at 11:05 PM
View Profile Send Email to User Show All Posts ICQ | Quote Reply
AndreaVB Forum : VB.Net : what is delegate and what is their use.
Previous Topic (CoolBar related question ?)Next Topic (Display pictures  on Forms in VB.NET) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder