473,587 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing events between different classes

Tom
This is probably a really basic question, but I know someone will be
able to help me. I have a single object A that contains a hash of
objects B. Each object B contains its own TCP client object containing
a connection to a server somewhere.

There is also a completely separate hash of objects C at the same
place as Object A. When I get TCP data into the TCP client I need to
let all of the object C's know about it. I Was wondering if I could
have all of the C objects register for the B events?

Tom

Jul 16 '07 #1
6 4489
On Mon, 16 Jul 2007 12:26:13 -0700, Tom <ku****@gmail.c omwrote:
This is probably a really basic question, but I know someone will be
able to help me. I have a single object A that contains a hash of
objects B. Each object B contains its own TCP client object containing
a connection to a server somewhere.

There is also a completely separate hash of objects C at the same
place as Object A. When I get TCP data into the TCP client I need to
let all of the object C's know about it. I Was wondering if I could
have all of the C objects register for the B events?
Is object A at all relevant? You didn't mention it with respect to the
communication between the objects B and objects C, so for the moment I'm
essentially (mostly) ignoring your first paragraph.

Anyway, yes...you can have each object C subscribe to an event on each
object B if you like. Every time an object C is instantiated, it would
have to subscribe to every object B's event, and every time an object B is
instantiated, each object C would need some way to know so that each can
subscribe to the new object B's event.

You may want to consider a "mediator"-based design, in which there's an
intermediate class that handles the connections between objects C and
objects B. Perhaps this is object A, or maybe you will want to make
another object to do this. You don't provide enough specifics for anyone
else to suggest which is better, so you'll have to decide that for
yourself.

The way the mediator would work is that objects C would all subscribe to a
single event on the mediator, while the mediator would subscribe to each
of the objects B event. Then when the mediator receives the event raised
by any object B, it would forward that event to its own event, which would
automatically result in each object C receiving the event.

This way each object C only needs to deal with a single object (the
mediator) and does not need to concern itself with the instantiation of
new objects B, while each object B would only have a single object (the
mediator) subscribed to its own event. Also, it encapsulates the
management of dealing with instantiations of each object B into a single
class (a nice way to do that would be to make the object B class aware of
the mediator, and register itself when it's instantiated, providing a
method in the mediator that in turn subscribes the mediator to the object
B event).

Pete
Jul 16 '07 #2
Hi,
"Tom" <ku****@gmail.c omwrote in message
news:11******** **************@ 22g2000hsm.goog legroups.com...
This is probably a really basic question, but I know someone will be
able to help me. I have a single object A that contains a hash of
objects B. Each object B contains its own TCP client object containing
a connection to a server somewhere.

There is also a completely separate hash of objects C at the same
place as Object A. When I get TCP data into the TCP client I need to
let all of the object C's know about it. I Was wondering if I could
have all of the C objects register for the B events?
You could do so, but then you will have to let each C instance know of every
B objects.

A better approach might be if A is the one that subscribe to B's and then A
will iterate in C's.

btw, how you are keeping receiving info from all those B's instances?
Jul 16 '07 #3
Tom
Thanks Peter,

This has really clarified everything for me, I guess I was just used
to doing things a certain way...

Thanks again,
Tom

Jul 17 '07 #4
Tom
I am feeling very frusturated about this right now. The part that is
frusturating is the:

// When the Publisher instance calls this, the Mediator
subscribes
to its event
public void Register(Publis her publisher)
{
publisher.TheEv ent += _MyHandler;
}
I have moved my mediator class to the utilites namespace of our
project. Both classes that need to use the mediator are in different
namespaces, but both use the utilities namespace. So that is great
and all until I get to the Register function. Based on the previous
example, show above, it looks like it needs to know about the
Publisher class. Is there a way to get around it having to know about
the publisher class? Since all my classes are USING ProgramUtils;, I
feel it would be improper to have USING MainProgram; in the utils?

Tom

Jul 26 '07 #5
On Thu, 26 Jul 2007 13:18:05 -0700, Tom <ku****@gmail.c omwrote:
[...] Based on the previous
example, show above, it looks like it needs to know about the
Publisher class. Is there a way to get around it having to know about
the publisher class?
You can do it a couple of ways:

1) Have a Publisher base class that the actual publisher class
inherits. The Publisher base class would be declared with the Mediator
class, while the actual publisher class can be declared anywhere, and the
Mediator and subscribers need not even know the actual publisher class.

2) Have a Publisher interface that the actual publisher class
implements. See above.

Pete
Jul 26 '07 #6
Tom
On Jul 26, 4:22 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Thu, 26 Jul 2007 13:18:05 -0700, Tom <kuh...@gmail.c omwrote:
[...] Based on the previous
example, show above, it looks like it needs to know about the
Publisher class. Is there a way to get around it having to know about
the publisher class?

You can do it a couple of ways:

1) Have a Publisher base class that the actual publisher class
inherits. The Publisher base class would be declared with the Mediator
class, while the actual publisher class can be declared anywhere, and the
Mediator and subscribers need not even know the actual publisher class.

2) Have a Publisher interface that the actual publisher class
implements. See above.

Pete

That seemed to work great. Thanks Again!!

Tom

Jul 26 '07 #7

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

Similar topics

14
12113
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming...
3
4745
by: Simon Harvey | last post by:
Hi, In my application I get lots of different sorts of information from databases. As such, a lot of information is stored in DataSets and DataTable objects. Up until now, I have been passing around chunks of data in DataTables/DataSets, simply because that was the format that they were in when the data was taken from the database. Now,...
4
1552
by: Stijn Goris | last post by:
Hi all, I place a usercontrol (2) on another usercontrol (1). The second usercontrol has a button. Is there a way usercontrol (1) can know when the button is clicked on usercontrol (2) without usercontrol (2) having an instance of usercontrol (1)? Reagrds Stijn
12
2779
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown when menu items are clicked.Besides these i would like to put a custom status bar. Any error message encountered in any of the webpage will be...
12
2673
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
30
3626
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment it seems to me that Microsoft has developed similar functionality via two keywords. I do understand that an event offers better encapsulation as the...
1
1410
by: dakboyan | last post by:
Hiya guys, I have a problem here that ive can't seem to solve on my own or through searching around the forums here. I would be glad if someone could assist me. This is actually an events management database that im helping to create for my school. First up is the calculation part. The idea is simple...for any 1 EVENT there are multiple...
5
2389
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how many heavy duty pro programmers in this newsgroup have actually used a delegate in their code, say in the last year of coding. By "use" I mean...
8
2077
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I created a user control that handles certain keystrokes, e.g. Ctrl-C for cut, Ctrl-V for paste, plus other more specialized keystrokes. I want to list these in the menubar like any other menu items. Once I assign a Shortcut Key to the menu item, that menu sees the event before the control, as one would expect. The question is, then, inside...
0
7915
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...
0
8205
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. ...
1
7967
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...
0
6619
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...
1
5712
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.