[.net] "Specified IComparer threw an exception"

Started by
3 comments, last by ernow 18 years, 10 months ago
Hey, I'm trying to get sorting into my engine using the native SortedList class of .NET (I don't know if this is the best way though). When I uncomment the line 'QueueItem(kid, cam);' an exception is thrown, but with it left commented out, it runs fine. The exception is: _innerException {"At least one object must implement IComparable." } _message "Specified IComparer threw an exception."

public void QueueItem(Node node, Camera cam)
{
	if(node != null)
	{
		if(node.Visible)
		{
			if(node is IRenderable)
			{
				float sqDist = (node.Location.X * cam.Location.X +
					node.Location.Y * cam.Location.Y +
					node.Location.Z * cam.Location.Z);
				itemList.Add(node, sqDist);
			}

			foreach(Node child in node.Children)
			{
			//	QueueItem(child, cam);
			}
		}
	}
}
Ollie"It is better to ask some of the questions than to know all the answers." ~ James Thurber[ mdxinfo | An iridescent tentacle | Game design patterns ]
Advertisement
I guess Node doesn't realize the IComparable interface... does it?

The SortedList uses the method Compare of the IComparable interface to sort objects. When an object in the list doesn't implement ICompareable this exception is thrown...

Check the MSDN on IComparable on how to implement it (e.g. nulls and different types should be compared in a specific way...)

Cheers
Have you specified an IComparer implementation upon creation of the list ? Or you can have your Node implement IComparable instead.

IComparer or IComparable are needed for the SortedList to be able to sort its element. Otherwise it has no way to tell what is the ordering of your custom elements.


That should do the trick,
jods
Dont worry - I got it, I was adding things the wrong way round (key = value, and value = key :P)
Ollie"It is better to ask some of the questions than to know all the answers." ~ James Thurber[ mdxinfo | An iridescent tentacle | Game design patterns ]
:) I should have noticed that!

BTW: The SortedList might do what you want it to do but there is one caveat I must give: The SortedList does not allow duplicate keys. So when two (particles?) have the same distance to the camera's position addition to the list fails...

Cheers

This topic is closed to new replies.

Advertisement