By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,277 Members | 1,433 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,277 IT Pros & Developers. It's quick & easy.

Comparing Objects with Option strict On

P: n/a
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.

Apr 11 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Erick wrote:
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
Apr 11 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.