By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,672 Members | 1,644 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,672 IT Pros & Developers. It's quick & easy.

Unique identifier in every treenode?

P: n/a
Is there some sort of unique identifier in every treenode
that is consistent? I was looking at the handle of every
treenode, but it appears that the handle changes everytime
the tree is built.

I have seen in some databases where they store the treenodes
they are using some serial number of the node. I don't have
a clue what that is, but can someone tell me if there is something
that is unique to every node.
Thanks

Rob
Jan 3 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Rob,

Well, within the confines of the program itself, then the reference
itself is unique, and you should be able to compare object references.

If you are talking about something you can persist, then you need to
look at other options. You need to associate that identifier with the node.

If the full path consisting of the label names represent a unique
identifier, then you can use the FullPath property on the TreeNode instance
to get the path from the root to that node. If not, then you will have to
assign something to the Tag property, creating the identifier and restoring
it when you recreate the tree from whatever underlying storage you are
using.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rob Stevens" <ro**********@sundry.comwrote in message
news:eH**************@TK2MSFTNGP06.phx.gbl...
Is there some sort of unique identifier in every treenode
that is consistent? I was looking at the handle of every
treenode, but it appears that the handle changes everytime
the tree is built.

I have seen in some databases where they store the treenodes
they are using some serial number of the node. I don't have
a clue what that is, but can someone tell me if there is something
that is unique to every node.
Thanks

Rob


Jan 3 '08 #2

P: n/a
Thanks Nicholas,

I ask this question because I have been looking at a lot of example code
within the
last week. And it's really confusing the way the tree is being stored to
whatever
storage is being used, and restored with the same exact layout still remains
a
mystery to me. I see no where in anyone's code where they are storing node
names,
levels, nothing. Here is an example I found last night, but even after
debugging and
looking at the values, I still can't see anything at all that shows me the
exact layout of
a tree.

void SaveTreeView(TreeView tree, string filename)
{
ArrayList al = new ArrayList();
foreach (TreeNode tn in tree.Nodes)
{
al.Add(node);
//listBox.Items.Add(node.Text + "," + depth);
}

Stream file = File.Open(filename, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
try
{
bf.Serialize(file, al);
}
catch (System.Runtime.Serialization.SerializationExcepti on e)
{
MessageBox.Show("Serialization failed : {0}", e.Message);
}
file.Close();
}

void LoadTreeView(TreeView tree, string filename)
{
if (File.Exists(filename))
{
Stream file = File.Open(filename, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
object obj = null;
try
{
obj = bf.Deserialize(file);
}
catch (System.Runtime.Serialization.SerializationExcepti on
e)
{
MessageBox.Show("De-Serialization failed : {0}",
e.Message);
}
file.Close();

ArrayList nodeList = obj as ArrayList;

// load Root-Nodes
foreach (TreeNode node in nodeList)
{
tree.Nodes.Add(node);
//listBox.Items.Add(node.Text);
}
}
}

I think until I figure out a lot of these underlying things in the code, I
will never understand what
I am looking at. I can't see individual properties being saved, its just a
mystery.

Rob
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uI*************@TK2MSFTNGP04.phx.gbl...
Rob,

If you are talking about something you can persist, then you need to
look at other options. You need to associate that identifier with the
node.

Jan 3 '08 #3

P: n/a
On Thu, 03 Jan 2008 12:04:29 -0800, Rob Stevens <ro**********@sundry.com>
wrote:
I ask this question because I have been looking at a lot of example code
within the
last week. And it's really confusing the way the tree is being stored
to whatever
storage is being used, and restored with the same exact layout still
remains a
mystery to me. I see no where in anyone's code where they are storing
node names,
levels, nothing. Here is an example I found last night, but even after
debugging and
looking at the values, I still can't see anything at all that shows me
the
exact layout of a tree.
That's because all the important stuff is hidden in the serialization code
for TreeNode.

I'm not really all that familiar with the specifics myself, but the code
you posted simply enumerates the root nodes, serializing each one. Then
in the "Load" method, it deserializes the stored data into an array of
TreeNodes, representing the root nodes, and adds each root node to the
tree.

Since TreeNode implements ISerializable and assuming the code you posted
works, it must include logic for serializing the entire structure under a
given TreeNode. To see specifically how it works, you would have to look
at the implementation for TreeNode itself, and/or the output from the
serializer (but you'd probably want to use a different formatter than
BinaryFormatter, so that you get something that's reasonable
human-readable). But there's nothing magical about it...it just means
that the code you posted is relying on an existing implementation that
already handles the job of relating the nodes somehow.

You could either take advantage of that, or write your own
implementation. Depending on your needs with respect to persisting the
data, either approach could be warranted.

Pete
Jan 3 '08 #4

P: n/a
Thanks Pete,

You have given me a lot of great ideas, now I have to put them to use.

Thanks again.

Rob
Jan 3 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.