DimkaNewtown Posted November 7, 2005 Posted November 7, 2005 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? Quote
Administrators PlausiblyDamp Posted November 7, 2005 Administrators Posted November 7, 2005 (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 November 7, 2005 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
DimkaNewtown Posted November 7, 2005 Author Posted November 7, 2005 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! Quote
bri189a Posted November 8, 2005 Posted November 8, 2005 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! :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.