-3

So we recently migrated an application from .NET 1.1 to .NET 4.0. And with that, there was a bunch of compatibility issues which we had to fix. One of them is that a block of code is throwing the InvalidOperationException.

Public Function MyFunction(ByVal Params As myParams, ByVal ParamArray someNumber As Integer()) As myData

   ...

   If someNumber.BinarySearch(options, MyEnum.Something) >= 0 Then
        ...
   EndIf

   ...

EndFunction

Before we migrated to .NET4 this was working correctly in .NET1. Now based on some threads i've been reading, there has been reports about this problem which was fixed in .NET4.5. And that to fix this in my current version, I have to implement the IComparable interface on all elements of the array.

How do I go about to fixing this? I would appreciate any help and pointer. Thanks!

EDIT: Adding the link to the BinarySearch method we are using in the code. https://msdn.microsoft.com/en-us/library/y15ef976.aspx

Smiley
  • 3,207
  • 13
  • 49
  • 66
  • Can we see the code of binary search that is doing the comparisons? – codemonkeyliketab Apr 08 '16 at 20:49
  • It's a built-in function from System.Array. – Smiley Apr 08 '16 at 20:57
  • Which one? We need more information to help you. – codemonkeyliketab Apr 08 '16 at 20:58
  • The BinarySearch is a System.Array method. https://msdn.microsoft.com/en-us/library/y15ef976.aspx – Smiley Apr 08 '16 at 21:07
  • 1
    `someNumber.BinarySearch(options, MyEnum.Something)` is technically an invalid statement, but it will compile. BinarySearch is a shared (static) method in the Array Class. The correct syntax is `Array.BinarySearch(arrayToSearch, itemToFind)`. If `options` is an integer array, you do not need to supply an IComparable as the Integer structure implements that interface. – TnTinMn Apr 12 '16 at 03:30

3 Answers3

0
  1. Add Implements IComparable IComparable Interface to your class definition. 2. Add a method for IComparable.CompareTo to the class. Borrowing from msdn:

    Public Class Temperature
        Implements IComparable
        ' The temperature value
        Protected temperatureF As Double
    
    Public Overloads Function CompareTo(ByVal obj As Object) As Integer _
    Implements IComparable.CompareTo
    
        If obj Is Nothing Then Return 1
        Dim otherTemperature As Temperature = TryCast(obj, Temperature)
        If otherTemperature IsNot Nothing Then
            Return Me.temperatureF.CompareTo(otherTemperature.temperatureF)
        Else
          Throw New ArgumentException("Object is not a Temperature")
        End If   
    End Function
    
    ....
    
    End Class
    

Of coarse the code in the CompareTo function depends on your class (you didn't provide much to go on). All numeric types (such as Int32 and Double) implement IComparable, as do String, Char, and DateTime. Custom types should also provide their own implementation of IComparable to enable object instances to be ordered or sorted. I believe that might be the situation in your case. I hope this helps.

JerryM
  • 910
  • 6
  • 9
0

Try this:

   ...
   Array.Sort(Of Integer)(someNumber)  ' only if someNumber is not previously sorted
   If Array.BinarySearch(Of Integer)(someNumber, MyEnum.Something) >= 0 Then
     ...
   End If
...

This should work in all .NET frameworks > 2.0.

LMR
  • 91
  • 5
0

How do I go about to fixing this?

You are not using it correctly. BinarySearch is a Shared/static method and doesnt show in Intellisense when trying to use it as an instance method:

enter image description here

If you type it in anyway, you get a new compiler warning: Access of shared member ... through an instance ... will not be evaluated. MSDN doesnt have anything for NET 1.1, so I dont know if it changed since then (doubtful). Correct usage:

IndexOf6 = Array.BinarySearch(myIntAry, 6)

Which begs the question, as part of the conversion from NET 1.x to 4.5, why not convert this to List(Of Int32). A quick test shows that the IndexOf() method is 2-3 times faster:

IndexOf6 = intList.IndexOf(6)

The List<T> method is also more 'standalone' since unlike a System.Array, it need not be sorted in order to work.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178