title: Type cast Java Object to my class
body: <p>Developing application in C# .Net 4.5 using Xamarin, targeting Android. I have a custom class that has some properties in it. I am trying to use a built in component that does comparison using Java.Util.IComparator and Java.Lang.Object. Because it is a built in component, I don't have much flexibility into changing those two items.</p>
  
  <p>My custom class is named recBatch and inside of it, I have some properties of integers and strings. </p>
  
  <p>This is where the component gets initialized. It basically calls a method each time the user clicks on the header for column 0.</p>
  
  <p><code>tableView.SetColumnComparator(0, GetBatchIdComparator());</code></p>
  
  <p>This is the method that gets called by the component</p>
  
  <p><code>public Java.Util.IComparator GetBatchIdComparator()
          {
              return new BatchIdComparator();
          }</code></p>
  
  <p>And finally, here is the class that is returned by the call.</p>
  
  <pre><code>public class BatchIdComparator : Java.Lang.Object, Java.Util.IComparator
  {
  
  
      public int Compare(Java.Lang.Object lhs, Java.Lang.Object rhs)
      {
          var leftID = (recBatch)lhs;
          var rightID = (recBatch)rhs;
  
          return leftID.Batch.CompareTo(rightID.Batch);
      }
  
  
  }
  </code></pre>
  
  <p>The first thing I tried to do above by just casting gives me an error as seen here. I did try what Visual Studio is suggesting but could not get it working either.</p>
  
  <p><a href="https://i.stack.imgur.com/hJdoN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hJdoN.png" alt="Error saying cannot cast from object to recBatch"></a></p>
  
  <p>The next thing I tried was to create a new class like this one and change the cast from recBatch, my actual class to this new class to do the casting:</p>
  
  <pre><code> public class BatchIdComparator : Java.Lang.Object, Java.Util.IComparator
  
   {
  
  
      public int Compare(Java.Lang.Object lhs, Java.Lang.Object rhs)
      {
          var leftID = (castClass)lhs;
          var rightID = (castClass)rhs;
  
          return leftID.BatchData.Batch.CompareTo(rightID.BatchData.Batch);
      }
  
  
  }
  
  public class castClass : Java.Lang.Object
  {
      public castClass(recBatch batchData)
      {
          batchData = BatchData;
      }
  
      public recBatch BatchData { get; private set; }
  }
  </code></pre>
  
  <p>With this, I don't have errors and can compile but the problem is I am getting a cast exception when I run. The code does compile and because I am casting, I do have access to one of the properties in recBatch (Batch or recBatch.Batch). However, again, I get a cast exception. The exact error is:</p>
  
  <p><a href="https://i.stack.imgur.com/7aL4h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7aL4h.png" alt="Cast Exception"></a></p>
  
  <p>So basically, I just need to cast the Java.Lang.Object into recBatch but I guess I am doing it wrong. Everything is "wired up" properly because if I put a break point at the Compare method, it does hit and the lhs, rhs arguments that are passed in have my class data in them (ie Batch) even though they are Java.Lang.Object types.</p>
  
  <p>Any help is appreciated!</p>
  
  <p>Thanks!</p>