Click here to Skip to main content
15,914,071 members
Home / Discussions / C#
   

C#

 
QuestionOperator Overloading inside an interface Pin
Butter18526-Jan-09 22:57
Butter18526-Jan-09 22:57 
AnswerRe: Operator Overloading inside an interface Pin
S. Senthil Kumar26-Jan-09 23:27
S. Senthil Kumar26-Jan-09 23:27 
AnswerRe: Operator Overloading inside an interface Pin
User 665826-Jan-09 23:30
User 665826-Jan-09 23:30 
QuestionRe: Operator Overloading inside an interface Pin
Butter18527-Jan-09 0:19
Butter18527-Jan-09 0:19 
AnswerRe: Operator Overloading inside an interface [modified] Pin
DaveyM6927-Jan-09 0:39
professionalDaveyM6927-Jan-09 0:39 
AnswerRe: Operator Overloading inside an interface Pin
N a v a n e e t h27-Jan-09 1:42
N a v a n e e t h27-Jan-09 1:42 
QuestionRe: Operator Overloading inside an interface Pin
Butter18527-Jan-09 4:08
Butter18527-Jan-09 4:08 
AnswerRe: Operator Overloading inside an interface [modified] Pin
DaveyM6927-Jan-09 7:52
professionalDaveyM6927-Jan-09 7:52 
You really should read the article[^] that I linked to previously, especially the section on Comparison Operators. All the information is in there.

Anyway, all you need to do is to implement overloads for < and > in your class. How you implement these is up to you, it very much depends on the data structure of your class and is not suitable for many classes. If you're implementing one of these operators, you MUST implement the second. The == and != operators are not necessarily required in your case, but if you choose to implement them you should override Equals and GetHashCode too.

Once you have < and > you can have your own CompareTo method that simply tests < and returns -1 if true, > and returns 1 if true, otherwise returns 0 (equal). I've given an example below. I've implemented IComparable too for completeness. The class just wraps a single Int32, obviously a real world example would be more complex.
using System;

public class MyClass : IComparable
{
    #region Constructors
    public MyClass(int value)
    {
        Value = value;
    }
    #endregion

    #region Properties
    public int Value
    {
        get;
        set;
    }
    #endregion

    #region Comparison Operator Overloads
    public static bool operator <(MyClass instanceA, MyClass instanceB)
    {
        return (instanceA.Value < instanceB.Value);
    }
    public static bool operator >(MyClass instanceA, MyClass instanceB)
    {
        return (instanceA.Value > instanceB.Value);
    }
    #endregion

    #region Compare Methods
    public int CompareTo(MyClass other)
    {
        if (this < other)
            return -1;
        else if (this > other)
            return 1;
        else
            return 0;
    }
    #endregion

    #region IComparable Members
    public int CompareTo(object obj)
    {
        if (obj is MyClass)
            return this.CompareTo((MyClass)obj);
        // return either -1 (less than) or 1 (greater than) here
        return 1;
    }
    #endregion
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

modified on Tuesday, January 27, 2009 2:10 PM

Questionend of string in C# Pin
lawrenceinba26-Jan-09 22:44
lawrenceinba26-Jan-09 22:44 
AnswerRe: end of string in C# Pin
SeMartens26-Jan-09 23:05
SeMartens26-Jan-09 23:05 
QuestionRe: end of string in C# Pin
Eddy Vluggen26-Jan-09 23:39
professionalEddy Vluggen26-Jan-09 23:39 
AnswerRe: end of string in C# Pin
lawrenceinba27-Jan-09 19:32
lawrenceinba27-Jan-09 19:32 
AnswerRe: end of string in C# Pin
Eddy Vluggen27-Jan-09 21:35
professionalEddy Vluggen27-Jan-09 21:35 
AnswerRe: end of string in C# Pin
Guffa27-Jan-09 13:28
Guffa27-Jan-09 13:28 
GeneralRe: end of string in C# Pin
lawrenceinba27-Jan-09 19:34
lawrenceinba27-Jan-09 19:34 
GeneralRe: end of string in C# Pin
Guffa27-Jan-09 22:15
Guffa27-Jan-09 22:15 
Questioncan we do row span and column span of c# datagrid Pin
s_aslam9826-Jan-09 22:25
s_aslam9826-Jan-09 22:25 
AnswerRe: can we do row span and column span of c# datagrid Pin
MadArtSoft27-Jan-09 1:44
MadArtSoft27-Jan-09 1:44 
QuestionURL into ClipBoard Pin
Member 587250026-Jan-09 21:44
Member 587250026-Jan-09 21:44 
AnswerRe: URL into ClipBoard Pin
benjymous26-Jan-09 22:19
benjymous26-Jan-09 22:19 
QuestionDictionary Classes Pin
Udayaraju26-Jan-09 21:29
Udayaraju26-Jan-09 21:29 
AnswerRe: Dictionary Classes Pin
S. Senthil Kumar26-Jan-09 21:34
S. Senthil Kumar26-Jan-09 21:34 
QuestionOutlook.MailItem Issues Pin
Sun Rays26-Jan-09 20:25
Sun Rays26-Jan-09 20:25 
AnswerRe: Outlook.MailItem Issues Pin
dennissal10-Mar-10 1:50
dennissal10-Mar-10 1:50 
GeneralRe: Outlook.MailItem Issues Pin
Ed Arnone2-May-11 8:15
Ed Arnone2-May-11 8:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.