Tim
Tim

Reputation: 561

Compare 2 objects to see if they are compatible in VB.NET

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.

Upvotes: 0

Views: 2676

Answers (6)

vvt
vvt

Reputation: 9

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

Upvotes: 0

Ben
Ben

Reputation: 3912

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.

Upvotes: 0

Konrad Rudolph
Konrad Rudolph

Reputation: 545628

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.

Upvotes: 0

Seth Moore
Seth Moore

Reputation: 3545

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

Upvotes: 1

Marcel Gheorghita
Marcel Gheorghita

Reputation: 1863

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

Upvotes: 2

Matthew Vines
Matthew Vines

Reputation: 27561

You want to implement the IComparer interface in your class.

The Compare Method is where the magic happens.

Upvotes: 1

Related Questions