Object identity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Klein

    Object identity

    A simple question hopefully...

    Can the Object GetHashCode() method be used to obtain a unique
    identifier for an instance of an object?

    To put this another way, what does the Object.Referenc eEquals() shared
    method use for its comparison?

    In Python, a unique identifier can be obtained with: id(object)

    Thanks,
    Daniel Klein
  • One Handed Man [ OHM# ]

    #2
    Re: Object identity

    I was asking a similar question, here it is and a good response I got from
    it.

    Regards - OHM

    -----------------
    The help for the .NET Framework Class Library tells us that the
    Object.GetHashC ode() Method does not guarantee uniqueness' or consistency
    and that overriding this and the Equals method is a good idea.

    It also tells us that using the XOR functions on two or more Fields or
    Properties is an acceptable way to achieve this.

    What do you guys think is the best approach to this given that generating a
    hashcode should be fast as well as consistant.
    ---------------

    OHM,
    When I'm defining classes that I want to be "HashTable friendly" I do both
    of what you quoted, the general template I follow is:

    Public NotInheritable Class KeyPair

    Private ReadOnly m_key1, m_key2 As Integer

    Public Sub New(ByVal key1 As Integer, ByVal key2 As Integer)
    m_key1 = key1
    m_key2 = key2
    End Sub

    Public Overrides Function GetHashCode() As Integer
    Return m_key1.GetHashC ode() Xor m_key2.GetHashC ode()
    End Function

    Public Overloads Function Equals(ByVal other As KeyPair) As Boolean
    Return m_key1 = other.m_key1 AndAlso m_key2 = other.m_key2
    End Function

    Public Overloads Overrides Function Equals(ByVal obj As Object) As
    Boolean
    If TypeOf obj Is KeyPair Then
    Return Me.Equals(Direc tCast(obj, KeyPair))
    Else
    Return False
    End If
    End Function

    End Class

    Note that there may be more "key" fields, and there may be non "key" fields
    also. Normally I make the "key" fields immutable (ReadOnly) to ensure that
    the HashCode does not change causing problems for the HashTable itself. In a
    couple cases I notify the container (HashTable) that a "key" is changing, so
    that it can remove the old key and add the new key.

    Remember that the HashCode gives the HashTable a slot to put the item into,
    that the HashTable then uses the Equals function to see if the item is
    already in that slot. In other words a single slot can have multiple items.
    Also there are other "requiremen ts" that make a good hash code, however I'm
    taking it on faith that the primitive types return good hash codes. By
    making my types build upon the primitive types, my types should also have
    fairly good hash codes...

    Hope this helps
    Jay

    "One Handed Man [ OHM# ]" <O_H_M{at}BTInt ernet{dot}com> wrote in message
    news:%23DO4qvs1 DHA.3140@tk2msf tngp13.phx.gbl. ..
    The help for the .NET Framework Class Library tells us that the
    Object.GetHashC ode() Method does not guarantee uniqueness' or consistency
    and that overriding this and the Equals method is a good idea.

    It also tells us that using the XOR functions on two or more Fields or
    Properties is an acceptable way to achieve this.

    What do you guys think is the best approach to this given that generating a
    hashcode should be fast as well as consistant.



    --


    Daniel Klein wrote:[color=blue]
    > A simple question hopefully...
    >
    > Can the Object GetHashCode() method be used to obtain a unique
    > identifier for an instance of an object?
    >
    > To put this another way, what does the Object.Referenc eEquals() shared
    > method use for its comparison?
    >
    > In Python, a unique identifier can be obtained with: id(object)
    >
    > Thanks,
    > Daniel Klein[/color]

    --
    Best Regards - OHM

    O_H_M{at}BTInte rnet{dot}com


    Comment

    • Mattias Sjögren

      #3
      Re: Object identity

      Daniel,
      [color=blue]
      >Can the Object GetHashCode() method be used to obtain a unique
      >identifier for an instance of an object?[/color]

      No

      [color=blue]
      >To put this another way, what does the Object.Referenc eEquals() shared
      >method use for its comparison?[/color]

      It does something like

      Return (obj1 Is obj2)



      Mattias

      --
      Mattias Sjögren [MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Object identity

        Daniel,[color=blue]
        > Can the Object GetHashCode() method be used to obtain a unique
        > identifier for an instance of an object?[/color]
        No, OHM gave all the reasons (I know of) that it cannot.
        [color=blue]
        > To put this another way, what does the Object.Referenc eEquals() shared
        > method use for its comparison?[/color]
        The actual "address" of the object on the heap.

        Remember that reference type variables (aka Objects) are actually a
        reference (pointer/memory address) to the object data itself on the heap,
        Object.Referenc eEquals compares these two pointers.
        [color=blue]
        > In Python, a unique identifier can be obtained with: id(object)[/color]
        After you get this unique identifier, what are you doing with it?

        I find for most reference types the object reference itself is sufficient
        for a unique identifier, normally when I need a different unique identifier,
        is when I am mapping the object back to a Database or something "concrete"
        that needs to be logically persisted as opposed to physically persisted (an
        ASP.NET session)...

        Hope this helps
        Jay

        "Daniel Klein" <danielk@aracne t.com> wrote in message
        news:0nr200lq2h p3jn4751jru9k7f csbk2s57r@4ax.c om...[color=blue]
        > A simple question hopefully...
        >
        > Can the Object GetHashCode() method be used to obtain a unique
        > identifier for an instance of an object?
        >
        > To put this another way, what does the Object.Referenc eEquals() shared
        > method use for its comparison?
        >
        > In Python, a unique identifier can be obtained with: id(object)
        >
        > Thanks,
        > Daniel Klein[/color]


        Comment

        • Daniel Klein

          #5
          Re: Object identity

          On Sun, 11 Jan 2004 12:52:58 -0600, "Jay B. Harlow [MVP - Outlook]"
          <Jay_Harlow_MVP @msn.com> wrote:
          [color=blue][color=green]
          >> In Python, a unique identifier can be obtained with: id(object)[/color]
          >After you get this unique identifier, what are you doing with it?[/color]

          The Python application I'm converting from is using it as a temporary
          database key. It's really no problem changing this to something like a
          sequential number.

          Thanks for the feedback,

          Daniel Klein

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Object identity

            Daniel,
            I would recommend either a sequential number of a System.GUID. The GUID
            would significantly reduce the chance of a key conflict.

            I don't know off hand of any built-in support for Python's id function.

            Hope this helps
            Jay

            "Daniel Klein" <danielk@aracne t.com> wrote in message
            news:1hu700lu6i v2ivl72tl042t52 jfiakfo03@4ax.c om...[color=blue]
            > On Sun, 11 Jan 2004 12:52:58 -0600, "Jay B. Harlow [MVP - Outlook]"
            > <Jay_Harlow_MVP @msn.com> wrote:
            >[color=green][color=darkred]
            > >> In Python, a unique identifier can be obtained with: id(object)[/color]
            > >After you get this unique identifier, what are you doing with it?[/color]
            >
            > The Python application I'm converting from is using it as a temporary
            > database key. It's really no problem changing this to something like a
            > sequential number.
            >
            > Thanks for the feedback,
            >
            > Daniel Klein[/color]


            Comment

            • Tom Shelton

              #7
              Re: Object identity

              In article <1hu700lu6iv2iv l72tl042t52jfia kfo03@4ax.com>, Daniel Klein wrote:[color=blue]
              > On Sun, 11 Jan 2004 12:52:58 -0600, "Jay B. Harlow [MVP - Outlook]"
              ><Jay_Harlow_MV P@msn.com> wrote:
              >[color=green][color=darkred]
              >>> In Python, a unique identifier can be obtained with: id(object)[/color]
              >>After you get this unique identifier, what are you doing with it?[/color]
              >
              > The Python application I'm converting from is using it as a temporary
              > database key. It's really no problem changing this to something like a
              > sequential number.
              >
              > Thanks for the feedback,
              >
              > Daniel Klein[/color]

              Question?... Does it really need to be converted from python?
              ActiveState has a python compiler for .NET (perl as well). I haven't
              tried it, but you may want to check it out :) Might save you a lot of
              trouble.

              --
              Tom Shelton
              MVP [Visual Basic]

              Comment

              Working...