Stack overflow with TreeNode property

  • Thread starter Thread starter The Kiddie
  • Start date Start date
T

The Kiddie

Hi Guys,
I am doing some work with a treenode and I want to record the
selected item, and it may change while the user is performing some
tasks and I need to know which one was originally selected.
I want to create a property which will store the selected
TreeNode, and I can refer back to that when it is time to make the
changes. However I get a stack overflow error when I assign to the
propery. Here's the code

private TreeNode OperationNode
{
get
{
return OperationNode;
}
set
{
OperationNode = value;
}
}

I try to assign using OperationNode = TreeView.SelectedNode, and the
stack overflows. Any idea would be appreciated. I have used properties
with strings and int's no problem before but this is the first time I
am using them with a complex type. I'm not sure if I have to create a
new object somewhere, I was playing with Clone, but that creates a
different object to the one i want. I supposed I could clone and then
go through the tree and use the object.Equals function, but there must
be an easier way than that.....

Cheers,

JD
 
The Kiddie wrote:
[...]
private TreeNode OperationNode
{
get
{
return OperationNode;
}
set
{
OperationNode = value;
}
}

I try to assign using OperationNode = TreeView.SelectedNode, and the
stack overflows.
[...]

You need to use an actual variable within your get { } and your set { },
instead of OperationNode. What you're doing here is using the same
property from within the property.
 
small typo

it should be:

private TreeNode operationNode = null;

private TreeNode OperationNode
{
get {return operationNode;}
set {operationNode = value;}
}

using OperationNode will just call the property which will cause a stack overflow.

Bill


Hello The,
 
Back
Top