How to Compare two objects regardless of data types in vb.net
As the title implies, I want to compare two objects whose type may be diffrent.
For eg, I expects 'true' for comparing 1000.0(Decimal) with 1000(Double) . Similary, it should return true if I compare 10(string) and 10(double) .
I tried to compare using Object.Equals() , but it did NOT work.It return false if t开发者_Python百科wo objects have different data types.
Dim oldVal As Object ' assgin some value
Dim newVal As Object 'assgin some value
If Not Object.Equals(oldVal,newVal) Then
'do something
End If
Edit: Could it be possible if I do the below?
1.check the type of oldVal
2.Covert the type of newVal to oldVal
3.Compare.
Try this:
Dim oldVal As Object 'assgin some value
Dim newVal As Object 'assgin some value
Dim b As Boolean = False
If IsNumeric(oldVal) And IsNumeric(newVal) Then
b = (Val(oldVal) = Val(newVal))
ElseIf IsDate(oldVal) And IsDate(newVal) Then
b = (CDate(oldVal) = CDate(newVal))
Else
b = (oldVal.ToString() = newVal.ToString())
End If
If Not b Then
'do something
End If
Or use IIf like this:
If Not CBool(IIf(IsNumeric(oldVal) And IsNumeric(newVal),
(Val(oldVal) = Val(newVal)),
IIf(IsDate(oldVal) And IsDate(newVal),
(CDate(oldVal) = CDate(newVal)),
(oldVal.ToString() = newVal.ToString())))) Then
'do something
End If
Mixing pears and apples? :) Equals does equality comparison (the eefault implementation of Equals tests for referential equality). While "=" does identity comparison.
If you need a different behaviour for comparing two objects, then define an Equals method and/or a CompareTo method.
To compare integers with doubles, you have to (implicitly or explicitly) convert one of the two operands.
dim i as integer = 1000
dim d as double = 1000.0
return i = CInt(d)
Object.Equals considers two value objects equal when they (a) have the same binary representation and (b) are bitwise equal, so basically it will always return false for different value types.
You can try to convert all values to double using Convert.ToDouble and then compare double values, if that suits your task; be ready to catch possible exceptions with conversion though.
You've basically described the standard behaviour of VB.Net if you use Option Strict Off
? So just use Option Strict Off
and then write a = b
In fact I recommend you do not do this as it is very likely to introduce bugs :) Can you explain why you need this behaviour?
there are a couple of ways you could do this, below is the one i feel that is closest to what you're looking for, though personally I dont think its a good way to structure your solution.
Dim fooObj As New Foo
Dim barObj As New Bar
fooObj.Value = 10
barObj.Value = 10
System.Diagnostics.Debug.WriteLine(barObj.Equals(fooObj))
System.Diagnostics.Debug.WriteLine(barObj.Equals(barObj))
System.Diagnostics.Debug.WriteLine(barObj.Equals(10))
System.Diagnostics.Debug.WriteLine(barObj.Equals(20))
System.Diagnostics.Debug.WriteLine(barObj.Equals("10"))
System.Diagnostics.Debug.WriteLine(barObj.Equals("20"))
System.Diagnostics.Debug.WriteLine(Object.Equals(barObj, 10))
Public Class Foo
Public Value As Integer
End Class
Public Class Bar
Public Value As Integer
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If TypeOf obj Is Bar Then
Dim compareBar As Bar = DirectCast(obj, Bar)
Return MyClass.Value = compareBar.Value
ElseIf TypeOf obj Is Foo Then
Dim compareFoo As Foo = DirectCast(obj, Foo)
Return MyClass.Value = compareFoo.Value
ElseIf TypeOf obj Is Integer Then
Dim compareInt As Integer = DirectCast(obj, Integer)
Return MyClass.Value = compareInt
ElseIf TypeOf obj Is String Then
Dim compareString As String = DirectCast(obj, String)
Return MyClass.Value.ToString() = compareString
End If
Return False
End Function
End Class
精彩评论