TreeView Tag and clone

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have TreeNodes in a TreeView, each contains unique data in its Tag section.
I am trying to 'clone' a TreeNode and then modify the tag data of the cloned
TreeNode. What I am seeing is that by modifying the Tag data of the cloned
TreeNode the original TreeNode's Tag data is also being modified with the
identical change.

Does the clone method not create a completely new data reference of the
original TreeNode? If not, how can I get a separate copy of the Tag data?
 
Clone creates a shallow copy. The Tag property is of type object, a
reference type. When you change the object pointed to by the reference, all
references to that object see the changes. To change the object for your
new TreeNode, create a new object and assign it to the Tag property of the
new TreeNode.

HTH

DalePres
MCAD, MCDBA, MCSE
 
If your object doesn't contain other reference types, you could just use
object.Clone() on your object. But you will have to handle it from the
lowest level of reference types and work up.

DalePres
 
I followed your suggestion and worked on this till quite late last night. I
now have routines that clone each object. I was fortunate that the arrays
within my objects use standard types so the clone() of the arrays went
smoothly. Again, thanks much!
 
Back
Top