Jump to content
  • Advertisement
Sign in to follow this  
acid2

[.net] "Specified IComparer threw an exception"

This topic is 4729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
			}
		}
	}
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Dont worry - I got it, I was adding things the wrong way round (key = value, and value = key :P)

Share this post


Link to post
Share on other sites
:) 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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!