rmatthew Posted March 5, 2006 Posted March 5, 2006 I have an array of a custom class that I have written. I have attempted to do an indexof to check for equality (i.e. is there a copy of the class already assigned to the array.) Array.IndexOf doesn't seem to work, and if I check for direct equality then I do not get what I would expect there either... Must I create an equality of some sort in the class it self? Quote
Cags Posted March 5, 2006 Posted March 5, 2006 Thats probably because you consider a class equal due to its values wheras by default .Net will check if the two classes reference the same block in memory. I believe the answer is to overide the Equals method of your class. Then use class1.Equals(class2) instead of class1 == class2. Quote Anybody looking for a graduate programmer (Midlands, England)?
Leaders snarfblam Posted March 5, 2006 Leaders Posted March 5, 2006 Do you want to see if they are the same object? If so, use Object.ReferenceEquals, or cast the objects to the Object type and use the == (or = in VB) operator. If you want to see if they have the same value, create your own Equals function or overload the Equals operator. Quote [sIGPIC]e[/sIGPIC]
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.