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

Unique, persistent numeric identifiers for TreeNodes?

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Kilroy Programmer" <ki***************@yahoo.com> wrote in message
news:11**************************@posting.google.c om...
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.

Nov 16 '05 #2

P: n/a
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.

ki***************@yahoo.com (Kilroy Programmer) wrote in message news:<11**************************@posting.google. com>...
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.

Nov 16 '05 #3

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

"Kilroy Programmer" wrote:
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.

ki***************@yahoo.com (Kilroy Programmer) wrote in message news:<11**************************@posting.google. com>...
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.

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.