Delegates help

M

Miro

I am trying to understand delegates and I think I do understand them ...
just hoping if someone can tell me im on the right track.
So far what I have read is that a Delegate is an Asynchronus call to a
method or function or whatever...

So basically you create an object that references the address of the
meathod.

By calling this delegate - you get a 'return' right away so your code can
continue and the delegate runs in a different thread doing whatever the
function / meathod is required.

Is this the correct assumption?
The example in the book is pretty simple ( its a writeline command ).

So as far as 'uses' for delegates ( if im on the right track )...its useful
when you have a lot to process, but do not want to do it in the main thread
( or the app will temporarily stop working ), you use a delegate to call the
meathod in a different thread.

Is this correct?

Thanks,

Miro
 
A

Adam Benson

Is this correct?
Not at all

I don't know if you're familiar with C or C++ (ignore this sentence if you
aren't) but if you are then the closest to them that you're familiar with
are function pointers.

In C# I guess you could think of them as a variable for method calls. Like
variables they have a type, and can be instantiated. An instance of a
delegate type can be called (invoked) and that will result in the method the
delegate instance points at being called.

So a delegate has a signature or type describing the kind of method it can
point to. A piece of code may say "If you want to know when I've done this
then I'll tell you - give me a delegate of such and such a type and I'll
call it when I've done it."

The bit about delegates executing on another thread - well, I don't know
where that came from, but a delegate call will execute on the same thread
that called it.

This is a bit of a loose definition, but I hope it gets you started.

Best way to figure this out is just to play with it. Create a form with a
button on and aim to pop up a message box when the button is clicked on.
That involves the use of delegates. Or type "delegate" in Visual Studio and
hit F1. That will call up the help for it.

Cheers,

Adam.
 
M

Miro

I might still be missing something but I see how the delegate points to the
function/method :

But I still seem to be missing why I wouldnt call the sub "DisplayMessage"
directly? Why delegate it?

Thanx,

Miro

=====
Public Class Form1

Delegate Sub MyFirstDelegation(ByVal blaTxt As String)


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim deleg As MyFirstDelegation
deleg = New MyFirstDelegation(AddressOf DisplayMessage)

deleg.Invoke("Why not just call display message directly")

Debug.WriteLine("when did this line get hit?")
End Sub

Sub DisplayMessage(ByVal msgText As String)
MessageBox.Show(msgText, "delegation")
End Sub

End Class
=====
 
J

Jon Skeet [C# MVP]

Miro said:
I might still be missing something but I see how the delegate points to the
function/method :

But I still seem to be missing why I wouldnt call the sub "DisplayMessage"
directly? Why delegate it?

In this case, you wouldn't. The point is to be able to effectively pass
some logic to other pieces of code.

See http://csharpindepth.com/Articles/Chapter5/Closures.aspx for an
article which concentrates on closures, but should also give you a
flavour of why delegates are a good thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top