Does any one know if C# has an equivalent of ObjPtr from VB6, or equivalent functionality (see more info below)? Here are a couple of links to info on ObjPtr devx , thevbzone.
Basically I have a third party treeview that I need to walk thru to get specific nodes but the only (relevant) info the nodes have is name ... but the node names don't need to be unique. So I need to get a unique value for each node as I walk thru it the first time so when I walk thru it again I know which is which. In the old school VB6 days I would use ObjPtr.
Any thoughts or suggests?
FK
If they're objects, why not just store the object references directly? These will be unique.
You can use Object.ReferenceEquals(x, y)
to determine if a reference you have stored is referring to the same object you just retrieved from the tree.
The closest direct equivalent I can think of would be to use a GCHandle to get an IntPtr for your object reference.
You would need to allocate a GCHandle for your object (GCHandle.Alloc), then use GCHandle.ToIntPtr to convert to an IntPtr. The linked documentation shows the process.
If the treenode has FullPath property, you can use it to uniquely identify a node in the treeview (Winforms Treeview has the FullPath property). This won't be unique if 2 siblings have same text in it.
OR
You could use Handle property of the TreeNode.
GetHashCode should work well for testing unique values unless the third-party has overriden the Object implementation with something that doesn't make sense in your scenario.
I would assume that nodes in the tree would define equality/hashcode by more than just the value string, but you would need to check.