Calling back to a specific object

C

Cheryl

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.Oncallback += new Server.callback(server_callback);
}
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.
 
P

Peter Duniho

Cheryl said:
[...]
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<> where 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.Callback()
{
}
}

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)testobjectInstance;

icallback.Callback();

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

Pete
 
C

Cheryl

Thanks!
Peter Duniho said:
Cheryl said:
[...]
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<> where 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.Callback()
{
}
}

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)testobjectInstance;

icallback.Callback();

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

Pete
 
G

Guest

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
 

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

Top