Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A question on TreeViews

0 views
Skip to first unread message

Dom

unread,
Dec 18, 2009, 12:12:45 PM12/18/09
to
I'm driving myself nuts with this TreeView. I don't want to get into
the error, but I want to give you what I think is the cause of the
error, and then see if anyone more knowledgable can confirm this.

In the statement ...

foreach (TreeNode n in MyTree.Nodes) { NewTree.Nodes.Add ((TreeNode)
n.Clone()); }

... I think I am just cloning the one node, and not it's children
nodes, or their children nodes, etc.

Now that I spell it out, that really seems right. Can anyone tell me
what I should be doing? Should I just make it all recursive?

Dom

Random

unread,
Dec 18, 2009, 1:00:57 PM12/18/09
to
On Dec 18, 9:12 am, Dom <dolivas...@gmail.com> wrote:
> ... I think I am just cloning the one node, and not it's children
> nodes, or their children nodes, etc.

Nope. The documentation for TreeView.Clone says:
"Copies the tree node and the entire subtree rooted at this tree
node."

> Now that I spell it out, that really seems right.  Can anyone tell me
> what I should be doing?  Should I just make it all recursive?

What exactly is your problem?

Peter Duniho

unread,
Dec 18, 2009, 1:35:57 PM12/18/09
to
Dom wrote:
> I'm driving myself nuts with this TreeView. I don't want to get into
> the error, but I want to give you what I think is the cause of the
> error, and then see if anyone more knowledgable can confirm this.
>
> In the statement ...
>
> foreach (TreeNode n in MyTree.Nodes) { NewTree.Nodes.Add ((TreeNode)
> n.Clone()); }
>
> .... I think I am just cloning the one node, and not it's children

> nodes, or their children nodes, etc.
>
> Now that I spell it out, that really seems right. Can anyone tell me
> what I should be doing? Should I just make it all recursive?

You probably shouldn't be using the Clone() method at all. But yes, the
Clone() method does what's known as a "shallow copy". That is, only the
object specifically being used to call Clone() is copied. That copy
winds up referencing all the same other objects that the original
referenced.

If you want the entire sub-tree of nodes to be cloned, you need to
recursively clone them manually. That said, it's more likely that the
right way to do this particular task is to not use the Clone() method at
all, and instead recursively re-create new TreeNode objects using the
pertinent data from the originals (e.g. the Value property). So much of
the new node's data will have to be different, I see little value in
actually copying it literally before you fix up everything else.

Pete

Dom

unread,
Dec 18, 2009, 1:49:35 PM12/18/09
to
On Dec 18, 1:35 pm, Peter Duniho <no.peted.s...@no.nwlink.spam.com>
wrote:

Thanks, I think I'll do the recursion without the Clone method. For
the record, the problem was that when I made changes that effected the
TAG property of the tree nodes in the clone, the TAG of the original
was changed also. Perhaps it is only the tag that needs to be created
new?

Peter Duniho

unread,
Dec 18, 2009, 2:06:28 PM12/18/09
to
Dom wrote:
> Thanks, I think I'll do the recursion without the Clone method. For
> the record, the problem was that when I made changes that effected the
> TAG property of the tree nodes in the clone, the TAG of the original
> was changed also. Perhaps it is only the tag that needs to be created
> new?

Probably. I overlooked that the TreeNode.Clone() method is specifically
implemented as a deep copy, but yes...even that deep copy can't clone
objects that themselves aren't cloneable. In fact, I wouldn't be
surprised if the _only_ thing about TreeNode.Clone() that's "deep" are
the other TreeNode children.

Pete

0 new messages