473,656 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Aug 6 '07 #1
3 1316
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
Aug 6 '07 #2
Thanks!
"Peter Duniho" <Np*********@Nn OwSlPiAnMk.com¦ b¶l¥ó
news:13******** *****@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
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.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.

Aug 6 '07 #4

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

Similar topics

2
3046
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 to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
30
2269
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 nested do loops, going through the records in rst using .movenext. At one point in one of the loops, we'll say the rst is at record "a". Now, another subprocedure is called, passing rst to it. In the subprocedure, the recordset goes through a...
0
1596
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 COM on the wrong thread, at which point things seem to go downhill very rapidly. I'm successfully making very heavy use of COM interop elsewhere in my C# code, but I'm having trouble finding a way around this particular problem. This is VS .Net 2003.
1
4031
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 provided with a set of Dll that implement the proxy needed to consume this web service. I'm now wrapping these dlls in a .Net webservice that can be consumed with Visual studio the familliar way.
6
5637
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 from. A has a pure virtual function virtual in f1() = 0. In B I implement this function and the compiler works out the code. However from A's destructor B can be compiled and then the runtime aborts with saying that a call to a pure virtual...
5
2241
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 method. The structure is like so: frmMain (MDI, runs on app start) calls frmB (MDI child), which in turn calls frmC (MDI child), which in turn calls frmD (MDI child). frmMain and frmB remain open while frmC and frmD are shown.
2
4238
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 parser under C++/CLI. It's a basic project created under Visual C++/CLR - Class Library - and it's pretty much what it is, just one /clr compiled class, fully CLR, no C++ native types or processing, nothing, just managed all the way. The reason...
47
4939
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 and my c++ code should call c code.but when i tried to call a function in c code externing that function in my c++ code, i am getting unresolved external symbol error. Whatever i try its giving more and more errrors...so is it possible to merge 2...
9
3248
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. Example function test (int a, int b);
0
8380
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.