2

I created a struct

 public struct MyCalender : IComparable<MyCalender>
{
 public int CompareTo(PersianDate other)
    {
        return DateTime.Compare(this, other);
    }
 .
 .
 .
 .
 .
}

I new two object of this in a other UserControl, and i want compare they.

I use this code but i get error.

 MyCalender value = new MyCalender(2010,11,12);
 MyCalender value2 = new MyCalender(2010,11,12);
        if (value < value2) ==> geterror
ar.gorgin
  • 4,765
  • 12
  • 61
  • 100
  • Overload the less than operator: http://stackoverflow.com/questions/9618500/needs-overload-operator-and-null-check – Tim Schmelter Oct 16 '12 at 14:02
  • 1
    You've created a `struct`, not a `class`. There are big differences between the two, and you almost certainly should *not* be creating a `struct`. – Adam Robinson Oct 16 '12 at 15:41

3 Answers3

5

IComparable exposes CompareTo. < and > must be overloaded separately:

class Foo : IComparable<Foo>
{
    private static readonly Foo Min = new Foo(Int32.MinValue);

    private readonly int value;

    public Foo(int value)
    {
        this.value = value;
    }

    public int CompareTo(Foo other)
    {
        return this.value.CompareTo((other ?? Min).value);
    }

    public static bool operator <(Foo a, Foo b)
    {
        return (a ?? Min).CompareTo(b) < 0;
    }

    public static bool operator >(Foo a, Foo b)
    {
        return (a ?? Min).CompareTo(b) > 0;
    }
}

I edited the code so that it does not fail when comparing against null. To keep it brief I used a shortcut that works unless value is Int32.MinValue for a proper Foo. Strictly speaking you'd have to check for null explicitly to get the contract right:

By definition, any object compares greater than (or follows) null, and two null references compare equal to each other.

Besides, implementing IComparable<T> means that CompareTo(T value) takes a parameter of T. Therefore MyCalendar : IComparable<MyCalender> should implement a method CompareTo(MyCalendar other) rather than PersianDate (or implement IComparable<PersianDate>).

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
  • thanks a lot, i use this code but when i use public Foo(int value) i get error" must be fully assigned before control is returned to the caller" – ar.gorgin Oct 16 '12 at 14:48
  • Did you initialize all `readonly` fields? The `Foo` class I wrote in VS2012 compiles, since `value` is initialized. – Matthias Meid Oct 16 '12 at 14:56
  • Yes it is readnly. I use vs2012 RC. I have 3 Initializes instance of the MyCalender structure. – ar.gorgin Oct 16 '12 at 15:02
  • Hang on, what did you do now? `Foo` is just an example class to show the principle. In `MyCalendar`, the operators need to get `MyCalendar` instances. Could you show us the code you've got now? – Matthias Meid Oct 16 '12 at 15:36
  • I added another paragraph about `MyCalendar` and `PersianDate`. I'm not sure whether `CompareTo` is implemented correctly in the first place. So, please show uf the full bit of relevant code :) – Matthias Meid Oct 16 '12 at 15:41
0

if comparing just a datetime object,

would something like

  DateTime A = DateTime.Now, B = DateTime.Now.AddMinutes(1);
  var isqual = A.Date.CompareTo(B.Date);

do the trick?

or something like:

        class Calender
        {
            public DateTime datetime { get; set;}
        }

        class DateComparer : Calender, IComparable<Calender>
        {
            public int CompareTo(Calender other)
            {
                return other.datetime.Date.CompareTo(this.datetime.Date);
            }
        }
Dean
  • 499
  • 6
  • 13
  • 34
0

You should either use CompareTo method that you already implemented instead of > in the line you posted or you need to overload > and < operators for your specific class. For instance:

public static bool operator >(MyCalendar c1, MyCalendar c2)
        {
            return c1.CompareTo(c2) > 0;
        }
        public static bool operator <(MyCalendar c1, MyCalendar c2)
        {
            return c1.CompareTo(c2) < 0;
        }

But keep in mind that you have to overload both of them.

Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33