At least one object must implement IComparable

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

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 PlayerList = new SortedSet();              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");                 }             }         }     } } 

So i keep getting the "

At least one object must implement IComparable.

" 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 requirement for the work, it's school work.

回答1:

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. So what sort order would you expect to see?

Basically, you need to tell your Player code how to compare one player with another. Alternatively, you could implement IComparer 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 {     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 players = new SortedSet(new PlayerNameComparer()); 


回答2:

You Player class needs to implement the IComparable interface..

http://msdn.microsoft.com/en-gb/library/system.icomparable.aspx



回答3:

Make your Player class implement IComparable.



回答4:

Your Player class must implement the IComparable interface. The SortedSet holds the items in a sorted order, but how would it know what the sorted order is if you haven't told it how to sort them (using IComparable)?



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!