Comparing Objects with Option strict On

E

Erick

I have a generic comparer class that i use to help me sort collections
of any object in any order.

It worked with Option strict off but now i need it to work with option
strict on.

Here is some of the code

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements System.Collections.IComparer.Compare

Dim prop As Reflection.PropertyInfo =
x.GetType.GetProperty(Me.SortProperty)
If Me.SortOrder = SortOrderEnum.None OrElse
object.Equals(prop.GetValue(x, Nothing),prop.GetValue(y, Nothing) Then
Return 0
Else

If prop.GetValue(x, Nothing) > prop.GetValue(y, Nothing)
Then

The last line won't work as X and Y are objects and you can't compare
objects with the > operand with option strict turned on.

Is there some way i can use Reflection to find the type and cast the
type in the If statement so i can check to see if one value is greater
than another ?

Any help appreciated.
 
A

Andrew Morton

Erick said:
I have a generic comparer class that i use to help me sort collections
of any object in any order.

It worked with Option strict off but now i need it to work with option
strict on.

Here is some of the code

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements System.Collections.IComparer.Compare

Dim prop As Reflection.PropertyInfo =
x.GetType.GetProperty(Me.SortProperty)
If Me.SortOrder = SortOrderEnum.None OrElse
object.Equals(prop.GetValue(x, Nothing),prop.GetValue(y, Nothing) Then
Return 0
Else

If prop.GetValue(x, Nothing) > prop.GetValue(y, Nothing)
Then

The last line won't work as X and Y are objects and you can't compare
objects with the > operand with option strict turned on.

Is there some way i can use Reflection to find the type and cast the
type in the If statement so i can check to see if one value is greater
than another ?

If you have to write a different line for each possible type of value, you
might just as well write a comparer class for each different class of
object. And it would compile :)

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top