Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted

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.

[sIGPIC]e[/sIGPIC]

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...