When converting a project (in which a template method of IComparable was used a few times) from VS 2005 to VS 2008 I’ve got some errors:
Error 12 Type argument 'Object' does not inherit from or implement
the constraint type 'System.IComparable'.
Is this an actual fact that System.Object no longer implements that interface, or something went wrong during the convertion? Can I fix this somehow?
The problem is with the following method:
Public Function ValueIn(Of T As IComparable)(ByVal pValue As T, ByVal ParamArray pArgs() As T) As Boolean
For Each MyArg As T In pArgs
If pValue.CompareTo(MyArg) = 0 Then
Return True
End If
Next
Return False
End Function
and even something simple like:
Dim a as Object = 1
ValueIn(a,1,2)
causes the error mentioned above. It worked perfectly in VS 2005, so what can be the problem now?
EDIT: I have just tried your code in both VS 2005 and 2008.
You have
Option Strict Offconfigured in your project or source code file. Your code never worked in the first place, and if you setOption Strict Onin VS 2005, you will see the real cause of the error, which is “Type argument inference failed for type parameter ‘T'”. I recommend thatOption Strict Onbe used in all VB.NET code.You see a different error in VS 2008 because it is using a newer version of the language, with very different overloading and type inference rules. In VB.NET 2008, the compiler cannot resolve the method call regardless of whether
Option Strictis on or off.The
System.Objecttype does not and has never implemented any interface.The setting of
Option Inferin VS 2008 is not relevant to your code because it does not make use of any inferred types.The simplest way to fix the error in both IDEs is to change the calling code thus: