开发者

Operator overloading?

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 lates开发者_运维技巧t 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?


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


As i tripped over a NullReferenceException, here's an improvement (well this may be subjective ;-)) to Daniel Hilgarth's answer.

The only change is that it handles nulls in case the first argument to the > or < operator is null:

public class SeasonEpisode : IComparable<SeasonEpisode>
{
    private static int Compare(SeasonEpisode e1, SeasonEpisode e2)
    {
        if (e1 == null && e2 == null)
            return 0;
        else if (e1 == null)
            return -1;
        else if (e2 == null)
            return 1;

        if(e1.Season == e2.Season)
        {
            if(e1.Episode == e2.Episode)
                return 0;
            else if(e1.Episode < e2.Episode)
                return -1;
            else
                return 1;
        }
        else if(e1.Season < e2.Season) 
            return -1;
        else
            return 1;
    }    

    public int CompareTo(SeasonEpisode other)
    {
        return Compare(this, other);
    }

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

    public static bool operator >(SeasonEpisode e1, SeasonEpisode e2) 
    {
        return Compare(e1, e2) > 0;
    }
}


You can implement the IComparer<T> interface

Defines a method that a type implements to compare two objects.

You can implement IComparable if you want a class to be comparable to another instance of that class. Which is probably what you want, in this case.

Implement IComparer if you need a class that compares two objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜