I am having some problem using SortedList in C# (I am working on Unity 5.0.3 with Visual Studio 2015). I have two classes ScoreKey and Score. ScoreKey implements IComparable.
But when I try to add entry to the SortedList I get the error
ArgumentException: element already exists
System.Collections.Generic.SortedList`2[ScoreKey,Score].PutImpl (.ScoreKey key, .Score value, Boolean overwrite)
I can't figure out why I am getting this error. I am using instances of an class as key so there is no chance of having the same key, right? Here is the code.
Class Definitions:
public class ScoreKey : IComparable
{
public uint val;
public uint timestamp;
public int CompareTo(object obj)
{
ScoreKey s2 = obj as ScoreKey;
if (s2.val == val)
{
return timestamp.CompareTo(s2.timestamp);
}
return val.CompareTo(s2.val);
}
}
[System.Serializable]
public class Score
{
public ScoreKey key;
public uint val
{
get { return key.val; }
}
string user;
public uint timestamp
{
get
{
return key.timestamp;
}
}
public Score(string _user, uint _score)
{
key = new ScoreKey();
key.timestamp = GetUTCTime();
user = _user;
key.val = _score;
}
}
Test Code:
SortedList<ScoreKey, Score> scoreList = new SortedList<ScoreKey, Score>();
Score[] scores = {
new Score("Bishal", 230),
new Score("Bishal", 3456),
new Score("Bishal", 230),
new Score("Bishal", 123),
new Score("Bishal", 86),
new Score("Bishal", 4221)
};
for(int i = 0; i< scores.Length; i++)
{
Debug.Log(scores[i].pretty);
scoreList.Add(scores[i].key, scores[i]);
}
EDIT:
**GetUTCTime Function: **
public static uint GetUTCTime()
{
return (uint)(System.DateTime.UtcNow.Subtract(new System.DateTime(1970, 1, 1))).TotalSeconds;
}