Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare two objects

Status
Not open for further replies.

rrhandle

Programmer
Dec 26, 2001
193
US
I have an object name oPart. It has a number of properties. I want to compare it to another oPart object to see if all the properties are the same. Been trying to use the IComparer thingy all day, but nothing works.

All I want to know is if oPart_new = oPart_old.

Thanks

 
Use:


dim oPart_new as object
dim oPart_old as object

If object.Equals(oPart_new, oPart_old) then
msgbox "Same!"
Else
msgbox "Different"
End IF
 
Yep. You will have to override equals to make sure it does what you want.

There are two Icomparables one that is used for generics and one for the normal arraylists. This one uses the generic one, but since that wasn't the question.

Code:
Public Class City
        Implements IComparable(Of City)

        Private strName As String

        '
        'This is the Name property.
        '
        Public Property Name() As String
            Get
                Return strname
            End Get
            Set(ByVal Value As String)
                strname = Value
            End Set
        End Property

        Public Sub New()
            strname = ""
        End Sub

        Public Sub New(byval Name as string)
            strname = name
        End Sub

        Public Function CompareTo(ByVal other As City) As Integer Implements System.IComparable(Of City).CompareTo
            If Not String.IsNullOrEmpty(Me.strname) Then
                If Not String.IsNullOrEmpty(other.strname) Then
                    Return Me.strname.CompareTo(other.strname)
                Else
                    Return 1
                End If
            Else
                If Not String.IsNullOrEmpty(other.strname) Then
                    Return -1
                Else
                    Return 0
                End If
            End If
        End Function

        Public Overrides Function Equals(ByVal obj As Object) As Boolean
            If obj.GetType Is GetType(City) Then
                If CType(obj, City).strname = Me.strname Then
                    Return True
                Else
                    Return False
                End If
            End If
            Return False
        End Function

        Public Overrides Function ToString() As String
            Return strname
        End Function

this is a class city where the name must be unique so I override equals to check for that.

You would need to override compareto if you needed it to sort in a different way.

Christiaan Baes
Belgium

"My new site" - Me
 
I think Rick's comment: "Doesn't .Equals check memory address" maybe the cause of my current problems, and Chrissie1's solution may not work either. I will restate the problem with an simple shopping cart example that allows the user to order various parts. This is not what the real application does, but I think the shopping cart example is one everyone can relate to.

I have an object named oPart.

It has the properties of: ItemNum, PartName, Length, and Depth.

As parts are chosen, they are placed in a collection named cPartsList.

Each time the user makes a change to their order, the old cPartList is destroyed and a new one is created.

Right before the old list is destroyed, a copy is created as cPartsList_Old.

After the new list is populated, I want to compare the two list to find parts that have been added or deleted.

Using Object.Equals fails to find any matching parts.

Question: What is the best way to finding matching parts in these two collections?

--Rob

 
I would say either use Chrissie's solution, or iterate through each collection and manually compare the properties that would identify the items as the same.

.Equals and = check memory address by default. So saying if Object1.equals(Object2) will check to see if Object1 and Object2 have the same memory address. Chrissie's solution attempts to avoid that problem by overriding the .equals method to make it compare the name of both objects instead of the memory address. In your version you would probably want to change that to compare ItemNum.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
So, if Chrissie's City object had a property fo Lat., Long., Population, and average income, I would have to compare each of those properties?

 
The same item can appear multiple times, but its properties can be different. With each part having 37 properties, I did not want to have to compare each property. I must be the only person who ever wanted to compare two objects. :-(

Thanks to everyone for your input.
Microsoft may suck, but Tek-Tips works!

 
And that's where Chrissie's code comes into play, you write the code to make those 37 comparisons once, in the Equals function, then you can call the Equals function, or use the iComparable implementation to not have to worry about re-writing that code.

Your other option would be to use reflection to get a list of properties and to run the comparisons dynamically. But you would likely want to set attributes on the properties that you could use to determine if the property should be compared or not. This method would not require the hard coding of all 37 comparisons, but it would require a good amount of work on learning reflection and developing a working solution.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Another thing you can do is override GetHashCode to have it compute based on the contents of your class, and do your comparison against that value.

But implementing IComparer or IComparable, either in your class or in a helper class, would be the better thing to do.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
What do you do if you just want to sort by the Integer value in an ArrayList of Structures? Say you have Structure that each contains a sort value of type Integer and they are in an ArrayList that you wish to sort.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
you will need a class and do this

Code:
Public Function CompareTo(ByVal other As City) As Integer Implements System.IComparable(Of City).CompareTo
      Return Me.intvalue.CompareTo(other.intvalue)
End Function

Christiaan Baes
Belgium

"My new site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top