Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator overloading?

Tags:

operators

c#

I've made myself a rss reader that keeps me up to date and informs me on new shows, or atleast thats the thought behind.

I've made a struct "SeasonEpisode" that hold two ints (season+episode) and a override ToString function.

I store the latest watched locally and i then read whats the newest is from the rss. But how could I compare SeasonEpisodes? right now I take each of the ints and compare them

if( se1.Season >= se2.Season )
    if( se1.Episode > se2.Episode || se1.Season > se2.Season )
        // new episode!

What i really want is

if( se1 > se2 )
    // new episode

Could i get any help please?

like image 884
Jason94 Avatar asked Apr 15 '11 06:04

Jason94


People also ask

What is operator overloading explain?

Polymorphism: Polymorphism (or operator overloading) is a manner in which OO systems allow the same operator name or symbol to be used for multiple operations. That is, it allows the operator symbol or name to be bound to more than one implementation of the operator. A simple example of this is the “+” sign.

What is operator overloading in C++?

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type.

What is overloading and operator overloading?

Function overloading means using a single name and giving more functionality to it. Operator overloading means adding extra functionality for a certain operator. When an operator is overloaded, the operator has different meanings, which depend on the type of its operands.

What is operator overloading and its rules?

1) Only built-in operators can be overloaded. New operators can not be created. 2) Arity of the operators cannot be changed. 3) Precedence and associativity of the operators cannot be changed.


1 Answers

There are two ways:

  1. Implement IComparable<T> and use CompareTo
  2. Overload the greater and less than operators

I suggest, you use both ways:

public class SeasonEpisode : IComparable<SeasonEpisode>
{
    public int CompareTo(SeasonEpisode other)
    {
        if(other == null)
            return 1;
        if(Season == other.Season)
        {
            if(Episode == other.Episode)
                return 0;
            else if(Episode < other.Episode)
                return -1;
            else
                return 1;
        }
        else if(Season < other.Season) 
            return -1;
        else
            return 1;
    }

    public static bool operator <(SeasonEpisode e1, SeasonEpisode e2) 
    {
        return e1.CompareTo(e2) < 0;
    }

    public static bool operator >(SeasonEpisode e1, SeasonEpisode e2) 
    {
        return e1.CompareTo(e2) > 0;
    }
}
like image 109
Daniel Hilgarth Avatar answered Oct 07 '22 04:10

Daniel Hilgarth