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
2
  • Overload the less than operator: stackoverflow.com/questions/9618500/… Commented Oct 16, 2012 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. Commented Oct 16, 2012 at 15:41

3 Answers 3

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>).

Sign up to request clarification or add additional context in comments.

5 Comments

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"
Did you initialize all readonly fields? The Foo class I wrote in VS2012 compiles, since value is initialized.
Yes it is readnly. I use vs2012 RC. I have 3 Initializes instance of the MyCalender structure.
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?
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 :)
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.