By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,768 Members | 1,895 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,768 IT Pros & Developers. It's quick & easy.

Pass external delegate to a class constructor.

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

Nov 17 '05 #2

P: n/a
Hi Brett,

Yes events will solve the thing, thank you :)
Marty

brett wrote:
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

Nov 17 '05 #3

P: n/a
Yes. Delegates are like function pointers in C and are designed for just
that.
"Marty" <xm******@hotmail.com> schrieb im Newsbeitrag
news:DjQBe.92755$wr.2889@clgrps12...
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

Nov 17 '05 #4

P: n/a
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
cody wrote:
Yes. Delegates are like function pointers in C and are designed for just
that.
"Marty" <xm******@hotmail.com> schrieb im Newsbeitrag
news:DjQBe.92755$wr.2889@clgrps12...
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


Nov 17 '05 #5

P: n/a
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();
}
}
"Marty" <xm******@hotmail.com> schrieb im Newsbeitrag
news:gk8De.95884$wr.13030@clgrps12...
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
cody wrote:
Yes. Delegates are like function pointers in C and are designed for just
that.
"Marty" <xm******@hotmail.com> schrieb im Newsbeitrag
news:DjQBe.92755$wr.2889@clgrps12...
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


Nov 17 '05 #6

P: n/a
Thank you, this is now clear to me :)

Regards,
Marty

cody wrote:
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();
}
}
"Marty" <xm******@hotmail.com> schrieb im Newsbeitrag
news:gk8De.95884$wr.13030@clgrps12...
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
cody wrote:

Yes. Delegates are like function pointers in C and are designed for just
that.
"Marty" <xm******@hotmail.com> schrieb im Newsbeitrag
news:DjQBe.92755$wr.2889@clgrps12...
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


Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.