locked
How to get instance ID of object RRS feed

  • Question

  • In VB6 I can use ObjPtr to get object pointer.  In .Net how to get it?

    Saturday, May 21, 2005 6:48 PM

Answers

  • The advice is to use a custom override for the GetHashCode() function in ur own classes to ensure that it is unique:

    MSDN Content:
    This method can be overridden by a derived class. Value classes must override this method to provide a hash function that is appropriate for the class and that ensures a better distribution in the hash table. Classes that might be used as a key in a hash table must also override this method, because objects that are used as keys in a hash table are required to generate their own hash code through this method.
    I guess there is no out-of-the-box support for this in .NET.

    I guess you could add a custom property to your classes and use the System.Guid class to generate a unique key for each of ur objects.

    Regards,
    Vikram

    Sunday, May 22, 2005 5:21 PM

All replies

  • Hi,

    The ObjPtr, VarPtr and StrPtr which were found in VB6 were really workarounds due to the limitations of Visual Basic before Visual Basic.NET.

    The MSDN documentation in fact says so as:
    VarPtr, VarPrtArray, VarPtrStringArray, ObjPtr and StrPtr were undocumented functions used to get the underlying memory address of variables. These functions are not supported in Visual Basic .NET.

    So if you could explain the scenario where you are using this, it would be easier to provide a solution.

    There are no direct equivalents. If all you are doing is using it to compare references then, you can use: ReferenceEquals()

    Regards,
    Vikram
    Saturday, May 21, 2005 8:34 PM
  • Hi, Thank you for your reply.

    My scenario is that I want to add object to a collection with Key

    Dictionory<string, object> objects=new Dictionory<string, Object);
    Test object=new Test();
    string key= ....//Get object ID here;
    objects.Add(key, object);
    Sunday, May 22, 2005 5:34 AM
  • Hi,

    See if you can use the GetHashCode() method which is available on every object. It returns a unique integer value for each instance of a class.


    object.GetHashCode();
     


    Regards,
    Vikram
    Sunday, May 22, 2005 7:14 AM
  • Maybe true in the current implementation but I've never read that the specs guarantee that GetHashCode() will be unique. Where did you get this info from?
    Sunday, May 22, 2005 11:18 AM
  • Hi Bruno,

    Thanks for pointing out. It is not **Unique**.

    Just did some check on GetHashCode():
    http://blogs.msdn.com/brada/archive/2003/09/30/50396.aspx
    So, it cannot after all be used as a unique id!

    Regards,
    Vikram
    Sunday, May 22, 2005 4:50 PM
  • No soluation in .Net?
    Sunday, May 22, 2005 5:03 PM
  • The advice is to use a custom override for the GetHashCode() function in ur own classes to ensure that it is unique:

    MSDN Content:
    This method can be overridden by a derived class. Value classes must override this method to provide a hash function that is appropriate for the class and that ensures a better distribution in the hash table. Classes that might be used as a key in a hash table must also override this method, because objects that are used as keys in a hash table are required to generate their own hash code through this method.
    I guess there is no out-of-the-box support for this in .NET.

    I guess you could add a custom property to your classes and use the System.Guid class to generate a unique key for each of ur objects.

    Regards,
    Vikram

    Sunday, May 22, 2005 5:21 PM
  • hell0

    Why are this functions no longer supported in Visual Basic.NET? I love to know.

    Monday, November 13, 2006 8:59 AM