Unique, persistent numeric identifiers for TreeNodes?

K

Kilroy Programmer

Is there a way to store a unique numeric identifier (say, for example,
an int) into a TreeNode, so that when the TreeNode is checked (since
CheckBoxes is enabled) the eventhandler AfterCheck() can examine the
responsible Node's identifier to see which TreeNode triggered the
event? Analyzing the Node's Text string is undesirable because it
would mean performing a string compare to a set of predefined strings.
This is slower and not easily maintained as the number of TreeNodes
increases.

Using GetHashCode() is undesirable because the resulting hash seems to
depend on the order in which the Nodes are checked.

I am hoping that after declaring a number of unique const ints in the
class constructor, I can later give each TreeNode its own unique
identifier as the TreeView is built. (Again, for easy lookup when
AfterCheck() is triggered). I need the solution to be persistent
across different instances of the program.

Does anyone have ideas on how to do this, or a different way to go
about what I'm trying to achieve? I've only been using C# for about a
week.
 
N

Nicholas Paldino [.NET/C# MVP]

Kilroy,

If you need persistent unique identifiers to be used across different
instances of the program, then I would go with Guids. I know they are not
numeric, but they are unique, and they persist fine. I assume you are
performing some sort of lookup in a hashtable, so these would work fine as
keys, and you can be sure that they are always unique, everywhere.

Hope this helps.
 
K

Kilroy Programmer

I will answer my own question. In retrospect it's obvious that
deriving from the TreeNode class and adding an instance variable that
holds a TreeNode's numeric identifier is a quick and easy way to get
this done.
 
G

Guest

Sorry if I have misunderstood, but could you not store the unique Id in the
"Tag" property of the node?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top