Results 1 to 8 of 8

Thread: [RESOLVED] Compare objects

  1. #1

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Resolved [RESOLVED] Compare objects

    How can i compare 2 custom objects (with the same definition)...???

    thx in advance...

  2. #2

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: Compare objects

    Code:
    Public Class Form1
    
        ''' <summary>
        ''' Handles the Load event of the Form1 control.
        ''' </summary>
        ''' <param name="sender">The source of the event.</param>
        ''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim myObject1 As New myObject
            Dim myObject2 As New myObject
    
            myObject1.Id = 1
            myObject2.Id = 1
    
            'compare the 2 objects
            Debug.Print(myObject1.Equals(myObject2))
    
        End Sub
    
    End Class
    
    Public Class myObject
    
        Private _id As System.Int32
    
        ''' <summary>
        ''' Gets or sets the id.
        ''' </summary>
        ''' <value>The id.</value>
        Public Property Id() As System.Int32
            Get
                Return _id
            End Get
            Set(ByVal value As System.Int32)
                _id = value
            End Set
        End Property
    
    End Class
    it returns false when the objects are the same... any help?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Compare objects

    The Object.Equals method tests whether two references refer to the same object. If you have created two objects then they can't possibly be the same object regardless of their property values, so Equals will return False.

    What you should be doing is implementing the IComparable interface and then you can implement your CompareTo method in whatever way is appropriate for your type. If you want to compare by Id then that's what you do. If CompareTo returns zero then the objects are equivalent.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: Compare objects

    Quote Originally Posted by jmcilhinney
    The Object.Equals method tests whether two references refer to the same object. If you have created two objects then they can't possibly be the same object regardless of their property values, so Equals will return False.

    What you should be doing is implementing the IComparable interface and then you can implement your CompareTo method in whatever way is appropriate for your type. If you want to compare by Id then that's what you do. If CompareTo returns zero then the objects are equivalent.
    thx for the tip...

    this is what i done:
    Code:
    Public Class Form1
    
        ''' <summary>
        ''' Handles the Load event of the Form1 control.
        ''' </summary>
        ''' <param name="sender">The source of the event.</param>
        ''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim myObject1 As New myObject
            Dim myObject2 As New myObject
    
            myObject1.Id = 1
            myObject2.Id = 2
    
            'compare the 2 objects
            Debug.Print(myObject1.Compare(myObject1, myObject2))
    
        End Sub
    
    End Class
    
    Public Class myObject : Implements IComparer
    
        Public Enum Status As Integer
            NotEqual = 0
            Equal = 1
        End Enum
    
        Private _id As System.Int32
    
        ''' <summary>
        ''' Gets or sets the id.
        ''' </summary>
        ''' <value>The id.</value>
        Public Property Id() As System.Int32
            Get
                Return _id
            End Get
            Set(ByVal value As System.Int32)
                _id = value
            End Set
        End Property
    
        ''' <summary>
        ''' Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        ''' </summary>
        ''' <param name="x">The first object to compare.</param>
        ''' <param name="y">The second object to compare.</param>
        ''' <returns>
        ''' Value Condition Less than zero x is less than y. Zero x equals y. Greater than zero x is greater than y.
        ''' </returns>
        ''' <exception cref="T:System.ArgumentException">Neither x nor y implements the <see cref="T:System.IComparable"></see> interface.-or- x and y are of different types and neither one can handle comparisons with the other. </exception>
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    
            Dim obj1 As myObject
            Dim obj2 As myObject
    
            obj1 = CType(x, myObject)
            obj2 = CType(y, myObject)
    
            If obj1.Id > obj2.Id Then
                Return Status.NotEqual
            ElseIf obj1.Id < obj2.Id Then
                Return Status.NotEqual
            Else
                Return Status.Equal
            End If
    
        End Function
    
    End Class
    made some tests and that solved my problem... thx...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Compare objects

    That is not the way it should be done. I said implement IComparable, NOT IComparer. You would NEVER make a class an IComparer for its own type. An IComparer is a class that exists specifically to compare two objects of another type, e.g.
    vb Code:
    1. Public Class Thing
    2.     '...
    3. End Class
    4.  
    5. Public Class ThingComparer
    6.     Implements IComparer
    7.  
    8.     '....
    9. End Class
    The ThingComparer.Compare method would compare two Thing instances. The only reason you would choose to do that is if you SPECIFICALLY needed an IComparer, like for sorting a ListView, or if you wanted to compare instances of a class that you couldn't change the implementation of that didn't already implement IComparable. The fact that this is your own class means that you should have it implement IComparable.

    Note also that if this is .NET 2.0 then you should implement the generic version of an interface if it is available. That way there's no possibility of that ArgumentException you've mentioned. Your code SHOULD look like this:
    vb Code:
    1. Public Class MyType
    2.     Implements IComparable(Of MyType)
    3.  
    4.     '....
    5.  
    6. End Class
    Just one final note. I assume that it is just for testing purposes but "myObject" is not a suitable name for a class. I say this because it shows a flaw in the way you think about classes. A class is NOT an object, so "myObject" is not an appropriate name. A class is a type, i.e. a description of a type of object. Each instance of the class is an object. This is the whole basis of OOP so it's very important to understand the distinction. Your class definition is just that: a definition. It is NOT an object. It's not until you invoke a constructor that you create an instance of the type, i.e. an object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: [RESOLVED] Compare objects

    Quote Originally Posted by jmcilhinney
    Just one final note. I assume that it is just for testing purposes but "myObject" is not a suitable name for a class. I say this because it shows a flaw in the way you think about classes. A class is NOT an object, so "myObject" is not an appropriate name. A class is a type, i.e. a description of a type of object. Each instance of the class is an object. This is the whole basis of OOP so it's very important to understand the distinction. Your class definition is just that: a definition. It is NOT an object. It's not until you invoke a constructor that you create an instance of the type, i.e. an object.
    i know what an object and a class definition is... thx for your care...

    this morning i was testing this code and found a better way in msdn that compares the current instance with another object of the same type:
    http://msdn2.microsoft.com/en-us/lib...to(vs.80).aspx

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Compare objects

    That MSDN link is for the IComparable.CompareTo method. I've been telling you to implement IComparable from the start. As I also said, you should be implementing the generic version of the interface in .NET 2.0. If you implement the standard IComparable interface then your code would look something like this:
    vb Code:
    1. Public Class Thing
    2.     Implements IComparable
    3.  
    4.     Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
    5.         If Not TypeOf obj Is Thing Then
    6.             Throw New ArgumentException("Object is not of type Thing.", "obj")
    7.         End If
    8.  
    9.         Dim otherThing As Thing = DirectCast(obj, Thing)
    10.  
    11.         'Compare Me with otherThing here.
    12.     End Function
    13.  
    14. End Class
    Note that the object being compared to is passed via an Object reference, which means that you must check its type at run time and allow for it to be the wrong type. If you implement the generic version of the interface that is taken care of at compile time.
    vb Code:
    1. Public Class Thing
    2.     Implements IComparable(Of Thing)
    3.  
    4.     Public Function CompareTo(ByVal other As Thing) As Integer Implements System.IComparable(Of Thing).CompareTo
    5.         'Compare Me with other here.
    6.     End Function
    7.  
    8. End Class
    It's not possible to pass any object that is not type Thing so you don't have to test its type or cast and there's no possibility of an exception.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: [RESOLVED] Compare objects

    Quote Originally Posted by jmcilhinney
    That MSDN link is for the IComparable.CompareTo method. I've been telling you to implement IComparable from the start. As I also said, you should be implementing the generic version of the interface in .NET 2.0. If you implement the standard IComparable interface then your code would look something like this:
    vb Code:
    1. Public Class Thing
    2.     Implements IComparable
    3.  
    4.     Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
    5.         If Not TypeOf obj Is Thing Then
    6.             Throw New ArgumentException("Object is not of type Thing.", "obj")
    7.         End If
    8.  
    9.         Dim otherThing As Thing = DirectCast(obj, Thing)
    10.  
    11.         'Compare Me with otherThing here.
    12.     End Function
    13.  
    14. End Class
    Note that the object being compared to is passed via an Object reference, which means that you must check its type at run time and allow for it to be the wrong type. If you implement the generic version of the interface that is taken care of at compile time.
    vb Code:
    1. Public Class Thing
    2.     Implements IComparable(Of Thing)
    3.  
    4.     Public Function CompareTo(ByVal other As Thing) As Integer Implements System.IComparable(Of Thing).CompareTo
    5.         'Compare Me with other here.
    6.     End Function
    7.  
    8. End Class
    It's not possible to pass any object that is not type Thing so you don't have to test its type or cast and there's no possibility of an exception.
    thanks for the code... i follow the example at the link and implemented the standard interface... i will change to a generic one, like you posted... thx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width