tags:

views:

220

answers:

6

I want to test if the 2 objects values can be compared.

For example if I have a variable define as an object which contains a value of "A" and another variable defined as an object with a value of 0. (These must be defined as type Object.)

When I do a simple compare If Object1 <> Object2 Then etc. I get an error. So how do I test to see if the objects are comparable? I have looked at TryCast , DirectCast, Ctype etc but cannot see how these can help.

Any suggestions.

+1  A: 

You want to implement the IComparer interface in your class.

The Compare Method is where the magic happens.

Matthew Vines
Thanks for the reply. I have tried this and get an error if the 2 objects are of different types.
Tim
+1  A: 

If you're just wanting to check and see if they're the same type then I think you could do this:

If Object1.GetType is Object2.GetTypeThen
    ...
End If
smoore
Thanks for the reply. This compares the 2 types but does not tell me if different types are comparable. for example a int16 and int64 are different types but are comparable.
Tim
+1  A: 

You should use ICompare and IComparable interfaces when defining your class. How to use ICompare and IComparable (MSDN)

Marcel Gheorghita
A: 

Another way to do this would be to overload the GetHashCode methods, to return a composite key for your object, then compare Object1.GetHashcode with Object2.GetHashCode. But realistically i would (as stated in the other answers) implement IComparable(Of T).

Edit -- Added sample code

Public Class MyClassA

    Private _myVariable As String = String.Empty
    Public Property MyProperty() As String
        Get
            Return "Fooey"
        End Get
        Set(ByVal value As String)
            _myVariable = value
        End Set
    End Property

    Public Overloads Overrides Function GetHashCode() As Integer
        Return MyProperty.GetHashCode
    End Function

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        Return (Me.GetHashCode = obj.GetHashCode)
    End Function

End Class

Public Class MyClassB

    Private _myVariable As String = String.Empty
    Public Property MyProperty() As String
        Get
            Return "Fooey"
        End Get
        Set(ByVal value As String)
            _myVariable = value
        End Set
    End Property

    Public Overloads Overrides Function GetHashCode() As Integer
        Return MyProperty.GetHashCode
    End Function

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        Return (Me.GetHashCode = obj.GetHashCode)
    End Function

End Class

Because you've Overriden (and Overloaded to return the type you want) the Equals Operator, and used the overidden GetHashCode method in it, you can you can do

If MyClassA.Equals(MyClassB) ......

All you then have to do is decide what to put in you GetHashCode method that would allow you to compare one object to another (the composite key).

Hope this helps.

Ben
Thanks, Any chance of some sample code. I am struggling with this. Regards
Tim
How is `GetHashCode` going to help at all? – Apart from the fact that your code doesn’t compile (`GetHashCode` returns an `Integer`, not a `String`, and your `Equals` method is broken).
Konrad Rudolph
I've changed the code, it did compile on mine as i didnt have option strict on (but admitadly i didnt try it). This will allow him to compare two of his objects.
Ben
Your edit unfortunately doesn’t make this code work correctly.
Konrad Rudolph
How so? I tried it -- If MyIntanciatedClassA.Equals(MyInstansiatedClassB) Then MessageBox.Show("Same") Else MessageBox.Show("Different") ... and it worked perfectly. Run as is, and it will show "Same", change one of the properties to return something different "Fooey2" and it will show "Different"
Ben
A: 

You may be able to use the Type.IsAssignableFrom method to test for type compatibility. Granted, this is about assignability rather than comparability but I guess this is the closest you’ll get.

FWIW, I would rather try to work around your requirement that both types be Object. Can’t you rather use some other common basetype which provides the mechanism for comparability? Just throwing plain Objects at the problem sounds like a design problem and has a distinct code smell.

Konrad Rudolph
A: 

is, gethashcode, equal compare reference only but compare value. Try IEqualityComparer. Hope this help

vvt