Equivalent (functionality) of ObjPtr from VB6 in C

2019-08-07 12:46发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.



标签: c# .net vb6