Extending TreeNodes

  • Thread starter Thread starter Michael C
  • Start date Start date
M

Michael C

Question about extending a class. I extended the TreeNode class, like this:

class ExtendedTreeNode : TreeNode
{
public int value1;
public int value2;
public int NodeLevel()
{ //This function returns the level (depth) of the node
...
}
}

This works all well and fine, except when I do something like the following:

private void Test()
{
int i = 0;
int j = 0;
TreeView MyTreeView = new TreeView();
ExtendedTreeNode tn = new ExtendedTreeNode();
tn.Text = "My Node";
tn.value1 = 10;
tn.value2 = 20;
MyTreeView.Nodes.Add((ExtendedTreeNode)tn.Clone());
i = ((ExtendedTreeNode)MyTreeView.Nodes[0]).value1;
j = ((ExtendedTreeNode)MyTreeView.Nodes[0]).value2;
Console.WriteLine(i);
Console.WriteLine(j);
}

I get 0, 0. The .Clone() method doesn't clone my extensions to the
ExtendedTreeNode class and I have to assign values to value1 and value2
after the fact. Is there any way to force .Clone() to clone my extensions
to the TreeNode class?

Thanks in advance,
Michael C.
 
Michael said:
I get 0, 0. The .Clone() method doesn't clone my extensions to the
ExtendedTreeNode class and I have to assign values to value1 and
value2 after the fact. Is there any way to force .Clone() to clone
my extensions to the TreeNode class?

You need to override the Clone() method to return your ExtendedTreeNode
and copy your new fields.
 
Jack Hanebach said:
Michael C wrote:

You need to override the Clone() method to return your ExtendedTreeNode
and copy your new fields.

I knew it was going to be something like that. Can anyone point me to a
tutorial for overriding the Clone() method? If I can figure out how to do
this, it'll greatly simplify my code!

Thanks,
Michael C.
 
Back
Top