Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 1st, 2006, 8:32 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 839
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Sharing objects?

I want to share a single instance of a Data object between two other objects, GraphPanel and ControlPanel. How can I do this neatly? Where do I store the object and how do the other objects reference it?

I could have the two classes extend a class which stores the data object, but I am looking for a more general approach. I thought of using an interface but I don't think that would work. Any ideas? Thanks.
titaniumdecoy is offline   Reply With Quote
Old May 1st, 2006, 8:59 PM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 292
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
You could always pass each of the classes a reference to the Data object
andro is offline   Reply With Quote
Old May 1st, 2006, 9:21 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 839
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
That's true, but what if I need to set the data object to a new object? Then the other classes will have references to the old objects.
titaniumdecoy is offline   Reply With Quote
Old May 1st, 2006, 9:54 PM   #4
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
union
__________________

tempest is offline   Reply With Quote
Old May 1st, 2006, 10:04 PM   #5
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Quote:
Originally Posted by titaniumdecoy
That's true, but what if I need to set the data object to a new object? Then the other classes will have references to the old objects.
Update the references when you instantiate a new object? setData or similar.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old May 1st, 2006, 10:37 PM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 839
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I'm looking for something a little neater, along the lines of accessing the data through a method... data().size(); for example.
titaniumdecoy is offline   Reply With Quote
Old May 2nd, 2006, 1:08 AM   #7
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 750
Rep Power: 3 Jimbo is on a distinguished road
Unless you're going to keep multiple of the shared objects around, why not just change the data of the original object? Does that work for your project?
Jimbo is offline   Reply With Quote
Old May 2nd, 2006, 5:16 PM   #8
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 839
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I want to be able to change the type of object, e.g., Data data = new TypeOfData;

I am still looking for a solution. The only way I can think of is to make the objects that use the data field subclasses of a new class.
titaniumdecoy is offline   Reply With Quote
Old May 2nd, 2006, 6:31 PM   #9
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 292
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
If you want the ability to change data types, make Data an Abstract class, and declare all other types of data from it.

Data d = new FancyDataType();

etc..
andro is offline   Reply With Quote
Old May 2nd, 2006, 7:57 PM   #10
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 839
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
The problem is storing a reference to the object; for example, if I have 2+ classes with a pointer to Data d and in one class I call d = new FancyDataType(), all the other classes will still reference the old data type. I'm trying to find the best way to make all the classes see the same variable.

Thanks for the help so far. If I figure something out I'll post it.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:44 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC