Click to See Complete Forum and Search --> : Copying objects in memory


nanode
01-30-2001, 01:04 PM
I think this problem should be language agnostic, but bare with me if it's not.

I have a set of objects in an application. Any of these objects can be cut, copy, paste and delete. For organization, I have each object instance stored in a Hashtable. This works well.

My problem is, when I 'copy' I am adding another instance of an existing object to the set.

So my collection looks something like:
A, B, C, D
If I copy B:
A, B, C, D, B

Problem is when I manipulate the last element 'B' it's also pointing to the first 'B'.

It's critical that the second 'B' have all the same properties as the first, but I simply need to de-reference it.

BTW: this is in Java

Any thoughts?

klamath
01-30-2001, 01:08 PM
Problem is when I manipulate the last element 'B' it's also pointing to the first 'B'.


So you want the 2 copies of B to have the same properties, but a change to B1 doesn't effect B2, and vice versa?

You might want to look into clone() -- with some persuasion, it might do what you're looking for.

------------------
- Klamath
Get my GnuPG Key Here (http://klamath.dyndns.org/mykey.asc)
Looking for an open source project to contribute to? Check out the Tornado HTTP Server (http://sourceforge.net/projects/tornado)

Energon
01-30-2001, 01:55 PM
You gotta remember that in Java, objects are nothing but pointers to memory, so when you copy them (even passing to functions), you get the address, not the object... the best thing to do would be to create a method like this:


MyClass copyClass(MyClass temp)
{
MyClass newOne = new MyClass();
newOne.field1 = temp.field1;
newOne.field2 = temp.field2;
// so on and so forth
return newOne;
}


It's the same basic idea as a C++ copy constructor, only in Java...

YaRness
01-30-2001, 02:09 PM
are you trying to simulate windows behaviour? (where you can copy or cut something, and then paste it back into the set)?

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

nanode
01-30-2001, 05:17 PM
Thanks for the replies.

I realize that Object variables are simply ref. to Objects and reassignment simply changes the reference.

I found a crude but effective way for this to work.


myButton.addActionListener(new ActionListener() { //new http://www.linuxnewbie.org/ubb/smile.gif
public void actionPerformed(ActionEvent e) {
CoolTable newTable = new CoolTable();
addToForm(newTable);
}
});



The scope is what saves me here. After the table instance is created, its variable loses scope outside of that actionListener. Since the 'addToForm()' method adds the table to a container, I can get to each unique instance later.

There is a side effect in my other method:


private void addToForm(JComponent j) {
DotWrapper dw = new DotWrapper(j);
dwArray.add(dw.getChild());
j.addPropertyChangeListener(getSelf());
j.addPropertyChangeListener(beanDirector);
j.addPropertyChangeListener(pg);
pg.addPropertyChangeListener((PropertyChangeListen er)j);
((IControl)j).setControlID(fdo.addControl((IContro l)j)); //add the component, not it's wrapper
if (j instanceof ITabIndex) {
((ITabIndex)j).setTabIndex(((IControl)j).getContro lID());
}
dw.addPropertyChangeListener(getSelf());
j.addPropertyChangeListener(dw);
formCanvas.getLayeredPane().add(dw, JLayeredPane.DRAG_LAYER, 0);
dw.sizeUP();
formCanvas.getLayeredPane().revalidate();
((IControl)j).announceProps();
dw.refresh();
}


The line DotWrapper dw = new DotWrapper(j); is bad. It causes dw to always and only reference the newest DotWrapper it's assigned to.

I'm going to try to initialize dw in the first method and then pass it to 'addToForm'.

As usual, I've solved one problem and created another. http://www.linuxnewbie.org/ubb/frown.gif