SortedList doesn't work for this case! Help!

Z

Zeng

Would someone know why SortedList doesn't work for these two keys?

[ArgumentException: Item has already been added. Key in dictionary:
"xyzbf8d7a21-d4c2-46a7-b005-a15d6ea3a20f" Key being added:
"xyz9097aa36-a64b-406b-8b27-33b42f4890b6"]
System.Collections.SortedList.Add(Object key, Object value) +221



My code looks something like below, the Guid.NewGuid().ToString() is
appended to make sure the key is unique in case of duplicated data.

foreach( CRecord rec in m_sortedList.Values )
{
tmplist.Add( rec.DataRow[fieldName].ToString() +
Guid.NewGuid().ToString(), rec );
}
 
J

Jon Skeet

Zeng said:
Would someone know why SortedList doesn't work for these two keys?

[ArgumentException: Item has already been added. Key in dictionary:
"xyzbf8d7a21-d4c2-46a7-b005-a15d6ea3a20f" Key being added:
"xyz9097aa36-a64b-406b-8b27-33b42f4890b6"]
System.Collections.SortedList.Add(Object key, Object value) +221

My code looks something like below, the Guid.NewGuid().ToString() is
appended to make sure the key is unique in case of duplicated data.

foreach( CRecord rec in m_sortedList.Values )
{
tmplist.Add( rec.DataRow[fieldName].ToString() +
Guid.NewGuid().ToString(), rec );
}

Looks odd to me. Could you write a short but complete program which
demonstrates the problem? Are you using a specific IComparer which
might be comparing those keys and saying they're equal?
 
Z

Zeng

You were right. The comparer I used said they were equal. I didn't realize
that SortedList used the comparer to determine the uniqueness of the key.
Thank you very much.



Jon Skeet said:
Zeng said:
Would someone know why SortedList doesn't work for these two keys?

[ArgumentException: Item has already been added. Key in dictionary:
"xyzbf8d7a21-d4c2-46a7-b005-a15d6ea3a20f" Key being added:
"xyz9097aa36-a64b-406b-8b27-33b42f4890b6"]
System.Collections.SortedList.Add(Object key, Object value) +221

My code looks something like below, the Guid.NewGuid().ToString() is
appended to make sure the key is unique in case of duplicated data.

foreach( CRecord rec in m_sortedList.Values )
{
tmplist.Add( rec.DataRow[fieldName].ToString() +
Guid.NewGuid().ToString(), rec );
}

Looks odd to me. Could you write a short but complete program which
demonstrates the problem? Are you using a specific IComparer which
might be comparing those keys and saying they're equal?
 
Top