Pass external delegate to a class constructor.

  • Thread starter Thread starter Marty
  • Start date Start date
M

Marty

Hi,

I would like to have my class to execute eventually an outside function.

I was thinking to pass to my class constructor a delegate object that
would be later used in my class to be triggered.

Does a delegate is the correct object to use to do that?

Thanks!
Marty
 
Marty,

The delegate will provide you with a pointer to another method, either
external or internal to your class depending on how your access is
setup. They are especially useful for events, which is what it sounds
like you want.

Brett
 
Hi cody,

I finally used delegate and events, but reading your comment, I'm
thinking that I could have done that without the events.

I saw example over the internet, but every time, the delegate
declaration was in a targetted class.

I would like the outside delegate callback function to be set within the
class constructor. Do you know how to do that?

something like:

class1:
myTriggerFunction() //This one to be used from class2.

class2:
constructor: record class1.myTriggerFunction() delegate address.
anotherFunction: trig recorded myTriggerFunction() delegate address.

Hope this give you an idea of want I want to do.
Thanks a lot!
Marty
 
something like that?

public delegate void MyDelegate(); // can be declared outside classes

public MyClass
{
MyDelegate d;

public MyClass(MyDelegate d)
{
this.d=d;
}

void Trigger()
{
if (d!=null)
d();
}
}
 

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

Back
Top