Delegate functions have to be static?

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

    Delegate functions have to be static?

    Hi,

    I've been looking at examples of delegates in C#. All the examples show
    delegates are references to static methods. Can delegates be used with
    instance methods?

    Thanks in advance,
    Bryan


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Delegate functions have to be static?

    Bryan,

    Yes, absolutely they can. All you have to do is when creating the
    delegate, pass the variable name with a reference, then the method name,
    like so:

    // Say that MyObject has a method that has the signature of the EventHandler
    delegate
    // called SomeMethod. You can do:
    MyObject pobjObject = new MyObject();

    // Create the event handler.
    EventHandler pobjHandler = new EventHanlder(po bjObject.SomeMe thod);

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Bryan Bullard" <reply@to.group .com> wrote in message
    news:%23fK%23ki 9UEHA.2972@TK2M SFTNGP11.phx.gb l...[color=blue]
    > Hi,
    >
    > I've been looking at examples of delegates in C#. All the examples show
    > delegates are references to static methods. Can delegates be used with
    > instance methods?
    >
    > Thanks in advance,
    > Bryan
    >
    >[/color]


    Comment

    • Shakir Hussain

      #3
      Re: Delegate functions have to be static?

      Bryan,

      Delegate can be mapped to static and instance methods.you may have to look
      this page.


      vcwlkdelegatest utorial.asp


      Shak.


      "Bryan Bullard" <reply@to.group .com> wrote in message
      news:#fK#ki9UEH A.2972@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Hi,
      >
      > I've been looking at examples of delegates in C#. All the examples show
      > delegates are references to static methods. Can delegates be used with
      > instance methods?
      >
      > Thanks in advance,
      > Bryan
      >
      >[/color]


      Comment

      • Bryan Bullard

        #4
        Re: Delegate functions have to be static?


        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
        message news:OwDy5j9UEH A.3788@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Bryan,
        >
        > Yes, absolutely they can. All you have to do is when creating the
        > delegate, pass the variable name with a reference, then the method name,
        > like so:
        >
        > // Say that MyObject has a method that has the signature of the[/color]
        EventHandler[color=blue]
        > delegate
        > // called SomeMethod. You can do:
        > MyObject pobjObject = new MyObject();
        >
        > // Create the event handler.
        > EventHandler pobjHandler = new EventHanlder(po bjObject.SomeMe thod);
        >
        > Hope this helps.[/color]

        ....

        Very helpful, thanks.


        Comment

        Working...