tags:

views:

227

answers:

4

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

A: 

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.

Eric Nicholson
-1. "The default implementation of the GetHashCode method does **not** guarantee unique return values for different objects." It is intended for *hashing* not to provide unique identifiers. From MSDN here: http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx
MarkJ
-1 Hashcode is not intended and for this and should never be used that way.
petr k.
+1  A: 

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.

shahkalpesh
Question does say it's a third-party treeview, so it may not have the standard properties
MarkJ
@Mark: And that is the reason, I start with "if the treenode has...." :)
shahkalpesh
A: 

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.

Reed Copsey
I think you're correct - this is the closest equivalent to ObjPtr - but I don't think FKCoder should be using this atom bomb to crack his particular walnut
MarkJ
I agree - but I was trying to answer the question directly, anyways.
Reed Copsey
+2  A: 

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.

Lasse V. Karlsen