的至少一个对象,必须实现IComparable [英] At least one object must implement IComparable

查看:1163
本文介绍了的至少一个对象,必须实现IComparable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedSet<Player> PlayerList = new SortedSet<Player>();

            while (true)
            {
                string Input;
                Console.WriteLine("What would you like to do?");
                Console.WriteLine("1. Create new player and score.");
                Console.WriteLine("2. Display Highscores.");
                Console.WriteLine("3. Write out to XML file.");
                Console.Write("Input Number: ");
                Input = Console.ReadLine();
                if (Input == "1")
                {
                    Player player = new Player();
                    string PlayerName;
                    string Score;

                    Console.WriteLine();
                    Console.WriteLine("-=CREATE NEW PLAYER=-");
                    Console.Write("Player name: ");
                    PlayerName = Console.ReadLine();
                    Console.Write("Player score: ");
                    Score = Console.ReadLine();

                    player.Name = PlayerName;
                    player.Score = Convert.ToInt32(Score);

                    //====================================
                    //ERROR OCCURS HERE
                    //====================================
                    PlayerList.Add(player);


                    Console.WriteLine("Player \"" + player.Name + "\" with the score of \"" + player.Score + "\" has been created successfully!" );
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("INVALID INPUT");
                }
            }
        }
    }
}

所以,我不断收到

至少有一个对象必须实现IComparable。

At least one object must implement IComparable.

尝试添加第二个球员的时候,第一个作品,但第二个没有。
我还必须使用的SortedSet ,因为这是工作的需要量,这是学校的工作。

" when trying to add a second player, the first one works, but the second one doesn't. I also MUST use SortedSet because that is the requirment for the work, it's school work.

推荐答案

好了,你想用的SortedSet<> ...这意味着您所关心的排序。但是,它的声音你的播放器类型不实现 IComparable的<玩家GT; 。所以,你希望看到什么样的顺序?

Well, you're trying to use SortedSet<>... which means you care about the ordering. But by the sounds of it your Player type doesn't implement IComparable<Player>. So what sort order would you expect to see?

基本上,你需要告诉你的播放器代码如何比较一个球员与另一个。或者,你可以实现的IComparer<播放器> 别的地方,而比较传入的构造的SortedSet<> ,以表明你想要什么样的顺序在球员例如,你可以有:

Basically, you need to tell your Player code how to compare one player with another. Alternatively, you could implement IComparer<Player> somewhere else, and pass that comparison into the constructor of SortedSet<> to indicate what order you want the players in. For example, you could have:

public class PlayerNameComparer : IComparer<Player>
{
    public int Compare(Player x, Player y)
    {
        // TODO: Handle x or y being null, or them not having names
        return x.Name.CompareTo(y.Name);
    }
}



然后:

Then:

// Note name change to follow conventions, and also to remove the
// implication that it's a list when it's actually a set...
SortedSet<Player> players = new SortedSet<Player>(new PlayerNameComparer());

这篇关于的至少一个对象,必须实现IComparable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆