2

I am fairly new to C# and Xamarin so hopefully this is something that is an easy answer.

I found myself in need of a reflective comparison method which could run over a varied bucked of objects. I based my solution off of the accepted answer in this question. Since I needed to be able to handle more than strict equality and use comparison operators if available, I added the following check

if (firstValue is System.IComparable)
{
    System.IComparable f = firstValue as System.IComparable;
    System.IComparable s = secondValue as System.IComparable;
...

so that I might be able to utilize the CompareTo method to know more than equality.

This works great for things such as numeric values, but it does not for the Xamarin generated Java.Util.Date and I would like some insight as to why.

namespace Java.Util
{
    public class Date : Object, ISerializable, ICloneable, IComparable, IJavaObject, IDisposable
    {
        ... 
        public virtual int CompareTo(Date date);
        ...
    }
}

From the definition of the is operator in the C# reference, it seems like this is exactly the case where is should evaluate to true.

I have checked and the IComparable that Java.Util.Date is implementing is System.IComparable.

I currently have a work around where I just call this method recursively on dates, but that is just a hack around my confusion.

Thank you for your help!

Community
  • 1
  • 1
  • You're saying `new Date() is IComparable` returns false? – Kirk Woll Nov 21 '14 at 14:57
  • That `IComparable` seems to be `Java.Lang.IComparable`, both are very different interfaces. – Sriram Sakthivel Nov 21 '14 at 15:00
  • Yes, @KirkWoll, `new Date() is IComparable` is false. @SriramSakthivel when I hover over the `IComparable` in the Date class's definition, it says `interface System.IComparable`. That was the first thing I checked. – mdellasalla Nov 21 '14 at 16:02
  • Oh, @SriramSakthivel if you are talking about where I am doing the actual casting, it is still `System.IComparable`. I am not using Java.Lang, so even though I made it explicit which I was checking in the if, the casts are not casting to a different `IComparable`. – mdellasalla Nov 21 '14 at 16:10
  • I was talking about the interface which was implemented by `Java.Util.Date` – Sriram Sakthivel Nov 21 '14 at 18:38
  • @SriramSakthivel I did check that it was implementing System.IComparable and noted it in the description of the problem. That was my first instinct as to what could be going wrong as well, but alas, it doesn't seem to be the case. – mdellasalla Nov 21 '14 at 18:39
  • I was referring to [this](http://androidapi.xamarin.com/index.aspx?link=M%3AJavax.Xml.Datatype.Duration.AddTo%28Java.Util.Date%29). If I was wrong, could you link the right doc? and also check what does `typeof(Date).GetInterfaces` returns? – Sriram Sakthivel Nov 21 '14 at 18:43
  • Ahh, good call. I have been relying on Visual Studio too much, it seems. It does seem to be the Java.Lang.IComparable in truth, but Visual Studio suggests that it is not. Thanks! – mdellasalla Nov 21 '14 at 18:59

0 Answers0