473,585 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using existing object when using New?

This is a bit tough to figure out the best way to ask, but here it goes.
I've got a class, say MyObject, and everytime this class is instanciated, I
actually want to get a reference to an existing object. Maybe better stated
by example:
public class SomeClassA {
MySpecialObject A;
public SomeClassA() {
this.A = new MySpecialObject ();
A.FireSpecialEv ent();
}
}

public class SomeClassB {
MySpecialObject B;
public SomeClassB() {
this.B = new MySpecialObject ();
this.B.SpecialE vent += new System.EventHan dler(this.B_Spe cialEvent);
}
private void B_SpecialEvent( object source, System.EventArg s e) {
// Do something here...
}
}
So, the trick is, that I want both MyObject's A and B to reference the same
object, so that when the line "A.FireSpecialE vent()" is called, it will
trigger the event handler in the SomeClassB object (assuming that the
constructor for SomeClassB has already been called). My problem up to this
point is that since A and B are actually referencing different objects, the
events don't fire across (of course).

Any ideas would be greatly appreciated. I'm hoping there's a way to maybe
override the 'new' method or something so that both A and B will end up
referencing the same object instance...? Or maybe there's a whole better way
to do this with some sort of global Event mechanism that I don't know about.

Thanks,
Chris
Nov 17 '05 #1
5 2642
Design patterns can be your friend and from the sounds of it... the Singleton
Pattern is exactly what you are looking for (shame it is the only one I know
well).

Take a look at this writeup over on MSDN on how it works:
http://msdn.microsoft.com/library/de...onInCsharp.asp

This example does not show exactly how to do what you are looking for...
however modifying it’s example into what (I think) you want will be a piece
of cake.

Brendan
"Christophe r D. Wiederspan" wrote:
This is a bit tough to figure out the best way to ask, but here it goes.
I've got a class, say MyObject, and everytime this class is instanciated, I
actually want to get a reference to an existing object. Maybe better stated
by example:
public class SomeClassA {
MySpecialObject A;
public SomeClassA() {
this.A = new MySpecialObject ();
A.FireSpecialEv ent();
}
}

public class SomeClassB {
MySpecialObject B;
public SomeClassB() {
this.B = new MySpecialObject ();
this.B.SpecialE vent += new System.EventHan dler(this.B_Spe cialEvent);
}
private void B_SpecialEvent( object source, System.EventArg s e) {
// Do something here...
}
}
So, the trick is, that I want both MyObject's A and B to reference the same
object, so that when the line "A.FireSpecialE vent()" is called, it will
trigger the event handler in the SomeClassB object (assuming that the
constructor for SomeClassB has already been called). My problem up to this
point is that since A and B are actually referencing different objects, the
events don't fire across (of course).

Any ideas would be greatly appreciated. I'm hoping there's a way to maybe
override the 'new' method or something so that both A and B will end up
referencing the same object instance...? Or maybe there's a whole better way
to do this with some sort of global Event mechanism that I don't know about.

Thanks,
Chris

Nov 17 '05 #2
I think that what you want is the Singleton pattern. Grab a copy of the
Gang of Four (Design Patterns by Erich Gamma, Richard Helm, Ralph
Johnson, and John Vlissides), or surf to Jon Skeet's page on Singleton
in C#:

http://www.yoda.arachsys.com/csharp/singleton.html

Essentially, what you want is to make MySpecialObject a singleton, so
that you say:

MySpecialObject A = MySpecialObject .Instance;
A.FireSpecialEv ent();

and elsewhere

MySpecialObject B = MySpecialObject .Instance;
B.FireSpecialEv ent();

and both A and B are the same object instance.

Nov 17 '05 #3
Actually, I've already looked at this design pattern, and it would be what I
need except for this: I'm being lazy and MySpecialObject is actually a class
that inherits from Component, so it shows up in my Toolbox when designing
Windows Forms. My goal was to be able to drop the component on two different
forms and then have the forms 'communicate' via a shared event mechanism.
Again, the lazy part of me is trying to avoid having to add code to my forms
that's not already in the InitializeCompo nent method created by Visual
Studio. So to use the Singleton Design Pattern, I would have to go into all
of my forms and do something to set the form's instance of the object back
to the Singleton object, and then re-connect the event handlers. I know it's
only a couple lines of extra code, but I've got several forms to keep
synched up and sticking to my hopeful plan would make it a bit easier.

Thanks,
Chris

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I think that what you want is the Singleton pattern. Grab a copy of the
Gang of Four (Design Patterns by Erich Gamma, Richard Helm, Ralph
Johnson, and John Vlissides), or surf to Jon Skeet's page on Singleton
in C#:

http://www.yoda.arachsys.com/csharp/singleton.html

Essentially, what you want is to make MySpecialObject a singleton, so
that you say:

MySpecialObject A = MySpecialObject .Instance;
A.FireSpecialEv ent();

and elsewhere

MySpecialObject B = MySpecialObject .Instance;
B.FireSpecialEv ent();

and both A and B are the same object instance.

Nov 17 '05 #4
Interesting case... One other option... which would depend on how the objects
are initialized... would be some remoting...

If you were to use RegisterWellKno wnServiceType() to register your class as
a Singleton WellKnownObject Mode... you could then register the class again
within the AppDomain with RegisterWellKno wnClientType().

Under this method... any following new instances of the registered class
will actually be remoted to a central, single instance of the class... this
would work... however I would highly advise against it.

Brendan
"Christophe r D. Wiederspan" wrote:
Actually, I've already looked at this design pattern, and it would be what I
need except for this: I'm being lazy and MySpecialObject is actually a class
that inherits from Component, so it shows up in my Toolbox when designing
Windows Forms. My goal was to be able to drop the component on two different
forms and then have the forms 'communicate' via a shared event mechanism.
Again, the lazy part of me is trying to avoid having to add code to my forms
that's not already in the InitializeCompo nent method created by Visual
Studio. So to use the Singleton Design Pattern, I would have to go into all
of my forms and do something to set the form's instance of the object back
to the Singleton object, and then re-connect the event handlers. I know it's
only a couple lines of extra code, but I've got several forms to keep
synched up and sticking to my hopeful plan would make it a bit easier.

Thanks,
Chris

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I think that what you want is the Singleton pattern. Grab a copy of the
Gang of Four (Design Patterns by Erich Gamma, Richard Helm, Ralph
Johnson, and John Vlissides), or surf to Jon Skeet's page on Singleton
in C#:

http://www.yoda.arachsys.com/csharp/singleton.html

Essentially, what you want is to make MySpecialObject a singleton, so
that you say:

MySpecialObject A = MySpecialObject .Instance;
A.FireSpecialEv ent();

and elsewhere

MySpecialObject B = MySpecialObject .Instance;
B.FireSpecialEv ent();

and both A and B are the same object instance.


Nov 17 '05 #5
Armed with this additional information, what I would suggest is that
you want to decouple your presentation from your model. I claim that,
in fact, you don't want your two components communicating with each
other. What you want is that each component is simply a visual
representation of the same model object, which contains the data being
displayed. Whenever the data changes, your model raises an event to
which your components listen and change the visual representation.
Whenever the user manipulates a control, the component changes the
model, which raises an event, which changes the visual representations
of the two components.

In other words, you need another class to act as a central holding bin
for your components' data, and have your controls interact with each
other through the model. You can use the IDE to connect the components
to the model's events if you make the model itself a Component.

The fancier version of this is called Model-View-Presenter.

Trust me, as complicated as it sounds, it's a lot simpler than trying
to make the two "views" talk to each other directly. I already went
down that route and found that the complexity soon spiraled out of
control.

Nov 17 '05 #6

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

Similar topics

1
6815
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must...
8
9840
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only...
4
12665
by: chris.dunigan | last post by:
I'm looking for an example of how to execute an existing DTS­ package from an ASP (VB)script and would appreciate any and all response. ­I don't even know if it's possible Thanks - Chuck Gatto Dan Guzman Apr 27 2000, 12:00 am show options
0
2329
by: Praveen | last post by:
Hello. I am writing some code that accepts a DFS Link and Username and grants that User permissions to the physical directory that the DFS Link corresponds to. I am using the System.Management namespace and WMI queries. When I run the code below and check the Security tab of the folder , I find that all entries have been cleared – which...
1
5287
by: Joe Bond | last post by:
Hi. I have a simple MS Access 2000 form in which I enter some customer data. When the address field is entered I need to see if a duplicate record exists. I need to know this *right away* before the remaining fields are filled out, so I'm trying to call a function on the Address_LostFocus() event which will do the lookup. If a match is found,...
1
2009
by: Michael Eisner | last post by:
I have an MS Access 8.0 (Office97) program that has a form called FO-008 that I need to replace on several users computers in different locations without me being there doing it manually. I'm trying to perform the replacement by using an external MS Access program that contains the replacement form also called FO-008. I need to keep the...
11
6579
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am...
1
4005
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the live databases on the network. Is there a way, via code, when I get back in-house from being on the road to click a button, and select the backends...
53
4650
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code, and .Net2005 code. I'm developing in vb.net 2005. This test sub just reads an input text file, writing out records to another text file, eliminating...
0
7908
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
8336
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...
1
7950
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
8212
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...
0
6606
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...
0
5389
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
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
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
0
1175
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.