Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a generic collection of custom objects. I tried implementing IComparable interface with the CompareTo method, but it doesn't actually fire when I do an IndexOf on the collection.

 

Note: I also tried IComparable<T> and had the same lack of results for the CompareTo method.

 

Anyone know what I'm doing wrong?

  • Administrators
Posted (edited)

Could you post the code, or if not could you post a sample that exhibts this behaviour?

 

Edit: also IIRC IndexOf will call the overriden Equals method rather than the IComparable interface, try overriding equals and simply get it to call your IComparable implementation.

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
Could you post the code, or if not could you post a sample that exhibts this behaviour?

 

Edit: also IIRC IndexOf will call the overriden Equals method rather than the IComparable interface, try overriding equals and simply get it to call your IComparable<T> implementation.

 

You were correct. I implemented the IEquatable<T> and it worked fine.

 

It's strange though because AFAIK the msdn help says you have to override the CompareTo method of IComparable interface.

 

Thanks!

Posted
Hope you left a comment on the MSDN page of the error, but thanks for discovering the error, I'll definitely stick that one in my head for when I one in to the same thing! :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...