Equivalent (functionality) of ObjPtr from VB6 in C#?

前提是你 提交于 2020-01-04 07:19:23

问题


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.


回答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.



来源:https://stackoverflow.com/questions/1374664/equivalent-functionality-of-objptr-from-vb6-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!