title: IComparable compare an integer with an object, cast did not work
body: <p>This is a part of a data science assignment, we don't get grades, just need to understand what's going on here. I've been at it for 10 hours straight and my TA gave up on me yesterday, so you're my last chance :-/ </p>
  
  <p>I'm learning about the <code>IComparable</code> interface. In that regard, I have a class, <code>ComparisonCountedInt</code>, and it's only job should be to take an integer (in the constructor), inherit <code>IComparable</code> and have a public static method which will count the amount of times a ComparisonCountedInt[] has been compared through the use of Binary Search.</p>
  
  <p>When running the code below I get a <code>System.ArgumentException</code>:</p>
  
  <blockquote>
    <p>System.ArgumentException : otherObj IS NOT COMPARABLE<br>
           at Library.ComparisonCountedInt.CompareTo(Object other)</p>
  </blockquote>
  
  <p>I've also tried to cast the object to an int but that didn't work either.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>public class ComparisonCountedInt : IComparable
  {
      // Holds the integer sat in the constructor
      public int Number;
  
      // Counter to see how many comparisons are made
      public int ComparisonCount { get; set; }
  
      // Input an integer
      public ComparisonCountedInt(int number)
      {
          this.Number = number;
          this.ComparisonCount = 0;
      }
  
      /// &lt;summary&gt;
      /// Should compare this.Number with another instances Number
      /// &lt;/summary&gt;
      /// &lt;param name="other"&gt;other is an object,  
      /// but we want to cast it to an integer&lt;/param&gt;
      public int CompareTo(object other)
      {
          if (other != null)
          {
              this.ComparisonCount++;
              ComparisonCountedInt otherObj = other as ComparisonCountedInt;
  
              if (otherObj != null)
              {
                  if (this.Number &gt; otherObj.Number)
                  {
                      return 1;
                  }
                  else if (this.Number == otherObj.Number)
                  {
                      return 0;
                  }
                  else
                  {
                      return -1;
                  }
              }
              throw new ArgumentException("otherObj IS NOT COMPARABLE");
          }
          else
          {
              throw new ArgumentException("other IS NOT COMPARABLE");
          }
      }
  
      /// &lt;summary&gt;
      /// Count how many comparisons that have been made
      /// (Uses Binary Search to compare the array)
      /// &lt;/summary&gt;
      public static int CountComparisons(ComparisonCountedInt[] array)
      {
          int sum = 0;
  
          for (int i = 0; i &lt; array.Length; i++)
          {
              sum += array[i].ComparisonCount;
          }
          return sum;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>The code below may not be needed for you but I thought I'd provide everything I call/use in the class in question.</p>
  
  <p><strong>To reproduce/testrun it</strong> I'm using a NUnit test:</p>
  
  <pre><code>[TestFixture]
  public class RunningTime
  {
      private IComparable[] _myArray;
  
      private readonly ComparisonCountedInt[] _comparisonCountedIntArray =
      {
          new ComparisonCountedInt(1),
          new ComparisonCountedInt(2),
          new ComparisonCountedInt(5),
          new ComparisonCountedInt(6),
          new ComparisonCountedInt(8)
      };
  
      [TestCase]
      public void TestRunTime()
      {
          // Reset the counter
          var comparisons = ComparisonCountedInt.CountComparisons(_comparisonCountedIntArray);
          comparisons = 0;
  
          Search.Binary((IComparable[]) _comparisonCountedIntArray,
              (IComparable) _comparisonCountedIntArray[2]);
  
          // Comparisons should be less or equal to base2
          var base2 = ((int)Math.Ceiling(Math.Log(_comparisonCountedIntArray.Length, 2.0)));
  
          Console.WriteLine("Comparisons: {0}", comparisons);
          Console.WriteLine("base2: {0}", base2);
  
          Assert.GreaterOrEqual(base2, comparisons);
      }
  }
  </code></pre>
  
  <p><strong>And the delivered binarySearch which uses compareTo (we had to make adjustments):</strong>  </p>
  
  <pre><code>public static class Search
  {
      public static int Binary(IComparable[] array, IComparable target)
      {
          // Checking if array is empty
          if (array.Length == 0)
          {
              return -1;
          }
  
          // Checking if array is out of bounds
          if ((target.CompareTo(array[0]) &lt;= 0) || (target.CompareTo(array.Length) &gt;= 0))
          {
              return -1;
          }
  
          var low = 0;
          var high = array.Length - 1;
  
          // Binary search loop starts here
          while (low &lt;= high)
          {
              var mid = low + (high - low) / 2; // var mid = (high + low) / 2;
              var midVal = array[mid];
              var relation = midVal.CompareTo(target);
  
              if (relation &lt; 0)
              {
                  low = mid + 1;
              }
              else if (relation &gt; 0)
              {
                  high = mid - 1;
              }
              else if (low != mid)
              {
                  //Equal but range is not fully scanned
                  high = mid; //Set upper bound to current number and rescan
              }
              else
              {
                  return mid;
              }
          }
  
          return -1;
      }
  }
  </code></pre>