Calling back to a specific object

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

    #1

    Calling back to a specific object

    Dear all,

    I have a question on implementing a callback function. Suppose I have a
    main class which contains a static class to communicate with a remote
    server.
    I also have an array of objects in which each of them can call and receive
    results from the static server class.
    class testobject
    {
    void testobject()
    {
    ServerClass.Onc allback += new Server.callback (server_callbac k);
    }
    void callserver()
    {...}

    void server_callback
    {
    }
    }

    However, with this design, all objects receive the callback once the server
    callback due to the multicasting property. If I want only the object which
    sends out the command to receive the callback, how should I implement that?

    Thanks.

  • Peter Duniho

    #2
    Re: Calling back to a specific object

    Cheryl wrote:
    [...]
    However, with this design, all objects receive the callback once the
    server callback due to the multicasting property. If I want only the
    object which sends out the command to receive the callback, how should I
    implement that?
    You would need a collection of delegate instances in the static class,
    maybe a Dictionary<wher e the key is the object with the delegate and
    the value is the delegate itself. Then given an instance of a
    testobject, you can use the testobject reference as the key for the
    Dictionary<to retrieve the callback delegate.

    If you are already keeping some sort of collection of the delegate
    instances anyway, then you could store the delegate from the instance in
    there as well somehow. Note, of course, that you simply cannot use the
    event paradigm in this case, since you only want to call a specific
    instance. You need to store the delegate reference as a per-instance of
    the testobject, and that's just not possible with the event.

    That said, it appears from your code that every testobject instance
    subscribes to the event. It seems to me that it might make more sense
    in this instance to declare a simple interface that the testobject class
    implements. Then the static class can just call that interface method
    instead.

    For example:

    static class ServerClass
    {
    public interface ICallback
    {
    public Callback();
    }
    }

    class testobject : ICallback
    {
    public ICallback.Callb ack()
    {
    }
    }

    Then in the ServerClass, when you would have had something like this:

    Server.callback handler = Oncallback;

    if (handler != null)
    {
    handler(...);
    }

    which winds up calling all of the testobject instances, you'd have
    something like this:

    ICallback icallback = (ICallback)test objectInstance;

    icallback.Callb ack();

    which would take a testobject instance references by
    "testobjectInst ance", get the interface that contains the callback
    method from it, and then call the Callback() method in the interface.

    Pete

    Comment

    • Cheryl

      #3
      Re: Calling back to a specific object

      Thanks!
      "Peter Duniho" <NpOeStPeAdM@Nn OwSlPiAnMk.com¦ b¶l¥ó
      news:13bd75u76i uoj68@corp.supe rnews.com ¤¤¼¶¼g...
      Cheryl wrote:
      >[...]
      >However, with this design, all objects receive the callback once the
      >server callback due to the multicasting property. If I want only the
      >object which sends out the command to receive the callback, how should I
      >implement that?
      >
      You would need a collection of delegate instances in the static class,
      maybe a Dictionary<wher e the key is the object with the delegate and
      the value is the delegate itself. Then given an instance of a
      testobject, you can use the testobject reference as the key for the
      Dictionary<to retrieve the callback delegate.
      >
      If you are already keeping some sort of collection of the delegate
      instances anyway, then you could store the delegate from the instance in
      there as well somehow. Note, of course, that you simply cannot use the
      event paradigm in this case, since you only want to call a specific
      instance. You need to store the delegate reference as a per-instance of
      the testobject, and that's just not possible with the event.
      >
      That said, it appears from your code that every testobject instance
      subscribes to the event. It seems to me that it might make more sense
      in this instance to declare a simple interface that the testobject class
      implements. Then the static class can just call that interface method
      instead.
      >
      For example:
      >
      static class ServerClass
      {
      public interface ICallback
      {
      public Callback();
      }
      }
      >
      class testobject : ICallback
      {
      public ICallback.Callb ack()
      {
      }
      }
      >
      Then in the ServerClass, when you would have had something like this:
      >
      Server.callback handler = Oncallback;
      >
      if (handler != null)
      {
      handler(...);
      }
      >
      which winds up calling all of the testobject instances, you'd have
      something like this:
      >
      ICallback icallback = (ICallback)test objectInstance;
      >
      icallback.Callb ack();
      >
      which would take a testobject instance references by
      "testobjectInst ance", get the interface that contains the callback
      method from it, and then call the Callback() method in the interface.
      >
      Pete

      Comment

      • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

        #4
        RE: Calling back to a specific object

        I think the simplest answer to this is to make the server class non static.
        Would this not be simpler. Unless there is another reason for it to be static


        --
        Ciaran O''Donnell



        "Cheryl" wrote:
        Dear all,
        >
        I have a question on implementing a callback function. Suppose I have a
        main class which contains a static class to communicate with a remote
        server.
        I also have an array of objects in which each of them can call and receive
        results from the static server class.
        class testobject
        {
        void testobject()
        {
        ServerClass.Onc allback += new Server.callback (server_callbac k);
        }
        void callserver()
        {...}
        >
        void server_callback
        {
        }
        }
        >
        However, with this design, all objects receive the callback once the server
        callback due to the multicasting property. If I want only the object which
        sends out the command to receive the callback, how should I implement that?
        >
        Thanks.
        >
        >

        Comment

        Working...