|
-
May 7th, 2007, 04:30 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Compare objects
How can i compare 2 custom objects (with the same definition)...???
thx in advance...
-
May 7th, 2007, 06:00 PM
#2
Thread Starter
Frenzied Member
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?
-
May 7th, 2007, 06:15 PM
#3
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.
-
May 7th, 2007, 06:29 PM
#4
Thread Starter
Frenzied Member
Re: Compare objects
 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...
-
May 7th, 2007, 06:58 PM
#5
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:
Public Class Thing
'...
End Class
Public Class ThingComparer
Implements IComparer
'....
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:
Public Class MyType
Implements IComparable(Of MyType)
'....
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.
-
May 8th, 2007, 12:27 PM
#6
Thread Starter
Frenzied Member
Re: [RESOLVED] Compare objects
 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
-
May 8th, 2007, 05:56 PM
#7
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:
Public Class Thing
Implements IComparable
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
If Not TypeOf obj Is Thing Then
Throw New ArgumentException("Object is not of type Thing.", "obj")
End If
Dim otherThing As Thing = DirectCast(obj, Thing)
'Compare Me with otherThing here.
End Function
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:
Public Class Thing
Implements IComparable(Of Thing)
Public Function CompareTo(ByVal other As Thing) As Integer Implements System.IComparable(Of Thing).CompareTo
'Compare Me with other here.
End Function
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.
-
May 9th, 2007, 03:13 PM
#8
Thread Starter
Frenzied Member
Re: [RESOLVED] Compare objects
 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:
Public Class Thing
Implements IComparable
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
If Not TypeOf obj Is Thing Then
Throw New ArgumentException("Object is not of type Thing.", "obj")
End If
Dim otherThing As Thing = DirectCast(obj, Thing)
'Compare Me with otherThing here.
End Function
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:
Public Class Thing
Implements IComparable(Of Thing)
Public Function CompareTo(ByVal other As Thing) As Integer Implements System.IComparable(Of Thing).CompareTo
'Compare Me with other here.
End Function
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|