473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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.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.

Aug 6 '07 #1
3 1309
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<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
Aug 6 '07 #2
Thanks!
"Peter Duniho" <Np*********@NnOwSlPiAnMk.com¦b¶l¥ó
news:13*************@corp.supernews.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<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
Aug 6 '07 #3
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
http://wannabedeveloper.spaces.live.com
"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.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.

Aug 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
30
by: Tim Marshall | last post by:
Here's the scenario, A2003, Jet back end, illustrated with some cut down code at the end of the post: A proc dims a snapshot recordset (dim rst as Dao.recordset) and opens it. There are several...
0
by: Ted Miller | last post by:
Hi folks, I originally posted this on microsoft.public.dotnet.framework.interop. Reposting to broaden the audience. I'm having an interop problem where my managed component is reentered from...
1
by: jens Jensen | last post by:
Hello , i'm calling a webservice generated with oracle webservice java tools. I'm not able to add a web reference to a .net client the usual way with visual studio 2005. I was therefore...
6
by: Henrik Goldman | last post by:
Hi I've had a problem with gcc mac osx which I think I figured out. I would like to double check with people here to see if my understanding is correct: I have a class A which class B inherit...
5
by: Earl | last post by:
I need to call a method on an owned child form, and am wondering if the best way of doing this is to capture the Closing event of the form that passes control back to the form where I have the...
2
by: Dragan | last post by:
Hi, We're working in VS 2005, Team edition, if it makes any difference at all (should be up-to-date and all that, but could not guarantee it is 100%). We've implemented a simple generic wrapper...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
9
by: Pubs | last post by:
Hi all, I want to call a function with some intial parameters with in a thread. At the end of the function execution it should return a value to the caller. Caller is outside the thread. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.