Pass external delegate to a class constructor.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marty

    Pass external delegate to a class constructor.

    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
  • brett

    #2
    Re: Pass external delegate to a class constructor.

    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

    Comment

    • Marty

      #3
      Re: Pass external delegate to a class constructor.

      Hi Brett,

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

      brett wrote:
      [color=blue]
      > 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
      >[/color]

      Comment

      • cody

        #4
        Re: Pass external delegate to a class constructor.

        Yes. Delegates are like function pointers in C and are designed for just
        that.


        "Marty" <xmarty99@hotma il.com> schrieb im Newsbeitrag
        news:DjQBe.9275 5$wr.2889@clgrp s12...[color=blue]
        > 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[/color]


        Comment

        • Marty

          #5
          Re: Pass external delegate to a class constructor.

          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:
          myTriggerFuncti on() //This one to be used from class2.

          class2:
          constructor: record class1.myTrigge rFunction() delegate address.
          anotherFunction : trig recorded myTriggerFuncti on() delegate address.

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


          cody wrote:
          [color=blue]
          > Yes. Delegates are like function pointers in C and are designed for just
          > that.
          >
          >
          > "Marty" <xmarty99@hotma il.com> schrieb im Newsbeitrag
          > news:DjQBe.9275 5$wr.2889@clgrp s12...
          >[color=green]
          >>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[/color]
          >
          >
          >[/color]

          Comment

          • cody

            #6
            Re: Pass external delegate to a class constructor.

            something like that?

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

            public MyClass
            {
            MyDelegate d;

            public MyClass(MyDeleg ate d)
            {
            this.d=d;
            }

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


            "Marty" <xmarty99@hotma il.com> schrieb im Newsbeitrag
            news:gk8De.9588 4$wr.13030@clgr ps12...[color=blue]
            > 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:
            > myTriggerFuncti on() //This one to be used from class2.
            >
            > class2:
            > constructor: record class1.myTrigge rFunction() delegate address.
            > anotherFunction : trig recorded myTriggerFuncti on() delegate address.
            >
            > Hope this give you an idea of want I want to do.
            > Thanks a lot!
            > Marty
            >
            >
            > cody wrote:
            >[color=green]
            > > Yes. Delegates are like function pointers in C and are designed for just
            > > that.
            > >
            > >
            > > "Marty" <xmarty99@hotma il.com> schrieb im Newsbeitrag
            > > news:DjQBe.9275 5$wr.2889@clgrp s12...
            > >[color=darkred]
            > >>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[/color]
            > >
            > >
            > >[/color][/color]


            Comment

            • Marty

              #7
              Re: Pass external delegate to a class constructor.

              Thank you, this is now clear to me :)

              Regards,
              Marty

              cody wrote:[color=blue]
              > something like that?
              >
              > public delegate void MyDelegate(); // can be declared outside classes
              >
              > public MyClass
              > {
              > MyDelegate d;
              >
              > public MyClass(MyDeleg ate d)
              > {
              > this.d=d;
              > }
              >
              > void Trigger()
              > {
              > if (d!=null)
              > d();
              > }
              > }
              >
              >
              > "Marty" <xmarty99@hotma il.com> schrieb im Newsbeitrag
              > news:gk8De.9588 4$wr.13030@clgr ps12...
              >[color=green]
              >>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:
              >>myTriggerFunc tion() //This one to be used from class2.
              >>
              >>class2:
              >>constructor : record class1.myTrigge rFunction() delegate address.
              >>anotherFuncti on: trig recorded myTriggerFuncti on() delegate address.
              >>
              >>Hope this give you an idea of want I want to do.
              >>Thanks a lot!
              >>Marty
              >>
              >>
              >>cody wrote:
              >>
              >>[color=darkred]
              >>>Yes. Delegates are like function pointers in C and are designed for just
              >>>that.
              >>>
              >>>
              >>>"Marty" <xmarty99@hotma il.com> schrieb im Newsbeitrag
              >>>news:DjQBe.9 2755$wr.2889@cl grps12...
              >>>
              >>>
              >>>>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
              >>>
              >>>
              >>>[/color][/color]
              >
              >[/color]

              Comment

              Working...