Type cast Java Object to my class

2016-06-22T08:03:27

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.

My custom class is named recBatch and inside of it, I have some properties of integers and strings.

This is where the component gets initialized. It basically calls a method each time the user clicks on the header for column 0.

tableView.SetColumnComparator(0, GetBatchIdComparator());

This is the method that gets called by the component

public Java.Util.IComparator GetBatchIdComparator() { return new BatchIdComparator(); }

And finally, here is the class that is returned by the call.

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);
    }


}

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.

Error saying cannot cast from object to recBatch

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:

 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; }
}

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:

Cast Exception

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.

Any help is appreciated!

Thanks!

Copyright License:
Author:「Michael Bedford」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/37956507/type-cast-java-object-to-my-class

About “Type cast Java Object to my class” questions

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...
I want to define a type cast from an arbitrary type to a primitive data type in Java. Is it possible to define a cast from one arbitrary type to another arbitrary type? public class Foo{ //met...
I'm looking for a simple solution to pass values of attributes/objects between two java programs. The programs are identical (running on separated nodes) and can't set/get variables though call of ...
I have a object: ExampleClass ex = new ExampleClass(); And: Type TargetType I would like to cast ex to type described by TargetType like this: Object o = (TargetType) ex; But when I do this ...
I have a class like this: public class Fields implements java.io.Serializable{ public short ID; public int SSN; public long Number; } and I have a hexadecimal string with the value like
We're trying to set up a secure IPC channel between an app and a service. To that end, we're providing an encrypt() and a decrypt() function that are used on both ends of the communication channel...
I'm wondering if there is a way to Auto Cast an Object to some type by storing the Class type along with the object? I thought this was possible with Java, but maybe not. For example: class Stora...
Java generates a proxy class for a given interface and provides the instance of the proxy class. But when we type cast the proxy object to our specific Object, how java handles this internally? Is ...
How to cast Java.Lang.Object to some native type? Example: ListView adapter contains instances of native type Message. When i am trying to get SelectedItem from ListView it returns instance of Me...
Is there a simple way to cast a SOAP object into a Java object (similar to converting JSON into Java object with objectmapper) ? The server side is using C# and has a class called "User Info" and ...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.