Delegates

  • Thread starter Thread starter web1110
  • Start date Start date
W

web1110

Hi,

Can someone provide me with an explanation of how delegates work. I know
how to use them by rote - meaning I look at code I have employing delegates
and reproduce it. My problem is that there is something about the process I
don't perceive. I think that somehow I've misinterpreted the word
"delegate" in this context and that has confused me somewhere. Or it could
be something else.

Thanx,
Bill
 
Delegate is nothing more than a typesafe managed function pointer wrapped in
framework class for you. Could you provide NG with more details about
problems you are having with the process? How are you using delegates that
does not seem to work for you the way you want it to.
 
Hi Bill,

I had the same problem than you in the past, and actually i use a the
delegates but i dont undestand it perfectly...

I will try maybe it could help you....

A delegate its a special type of class that acts like a pointer to any
method in another class.

Too allows to you create events of type of the delegate for instance, ect...

Here is an example:

//here you are stablishing a delegate that
public delegate void stringhandler(string text);


///////////////////////////////a class with one event////////////////////////
public class text
{
//define the event of type stringhandler
public event stringhandler textchanged;
//define the method that will be raise the event
public virtual void ontextchanged(string text)
{
if(textchanged!=null)
{
textchanged(text);
}
}
public void changetext(string text)
{
//here i change the text when the class is instanciated
text="Hello world";
//before i change the text to hello world, and then i call to the
method that
//raise the event
ontextchanged(text);
}
}
////////////a class subscribed to the event of the previous
class///////////////////
public class subscriber
{
public subscriber()
{
//make an instance of the class text
text exampletext=new text();
//subscribe to the event using the delegate and a method name in
this class
exampletext.textchanged +=new stringhandler(controltheevent)
//now call to the method to change the text (the event will be
raised, and will
//be executed the next method
exampletext.changetext("Hello");

}
//as you see its like were created a link between the method
Ontextchanged that
//raise the event and this method that has the same features that the
delegate
//(parameters)
private void ProductsProcs_ProductFinded(string text)
{
Console.WriteLine(text);
}
}
Hope this helps Bill.

Kind Regards.
Josema.
 
Sorry the name of the last method of the last class is:

private void controltheevent(string text)
{
Console.WriteLine(text);
}

instead Products_.-........

I copy and paste code, and i forget to replace this part...
 
Hi again,



Thank you all. I was reading 'delegate' as a verb, not as a noun. That
just made things foggy. Is this interpretation corrct?



Delegate interpretation:



Declare a delegate type



public delegate void MyEvent(object sender, EventArgs e);



This says that MyEvent is a delegate type that can encapsulate a method that
returns void and takes 2 parameters, an object and EventArgs. It is a model
for a delegate declaration.



Declare a variable of the delegate type



public event MyEvent MyEventhandler;



This is the actual delegate that will contain the actual delegated method



Declare a 'to be' delegated method



private void MyEventCatcher(object sender, EventArgs e)

{

MessageBox.Show("Triggered");

}



Instantiate a delegate and encapsulate the method with in it



MyEventHandler=new MyEvent(MyEventCatcher);



Invoke the delegate



MyEventHandler(this, some_event);



This calls MyEventHandler and passes through it to MyEventCatcher



One last assertion:



I am assuming that a delegete cannot be assigned a list of methods like an
event handler. This doesn't make sense to me. Am I right?



Thanx,

Bill
 
web1110 said:
Yes, a delegate can be assigned multiple delegates - they must all return
void.

No, they all need to have the same signature, but that could be a non-
void one. If so, and the return value is used, it's the last return
value in the chain which is returned:

using System;

public class Test
{
delegate int FooDelegate (int x);

static void Main()
{
FooDelegate t = new FooDelegate (Double);
t += new FooDelegate(Treble);

Console.WriteLine (t(4));
}

static int Double (int x)
{
return x*2;
}

static int Treble (int x)
{
return x*3;
}
}
 
And it is possible to manually invoke each of the delegates and examine
each return value. See GetInvocationList();

Regards,
Jeff
 
Back
Top