Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Zipster

Getting an ArrayList to use my Equals

This topic is 5276 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

I''ve been having problems with this for some time now, and while I''ve usually been able to write a workaround it''s gotten to the point where I''m downright annoyed. I have a custom object Texture with the following overloaded Equals:
public override bool Equals(object obj)
{
   if(obj is string)
   {
      return Name.Equals((string)obj);
   }
 
   return false;
}
 
This is so I can check to see if a Texture object is in an ArrayList using Contains, just by passing the name of the texture. However, the ArrayList doesn''t use my overloaded Equals. I''ve been pulling my hair out over this and have come to the conclusion that it''s probably using the string''s Equals function and passing the Texture object, rather than the other way around. Either that, or using the static Equals function, but still calling it on the string object internally. My last resort is to pass a dummy Texture object with just the name initialized for the comparison, but that simply blows. I would hang myself for doing something like that. Someone must know what''s going on with this ArrayList. Or at least a convenient way to resolve my dilemma.

Share this post


Link to post
Share on other sites
Advertisement
If you want to be able to look up Textures by a String key, why not use a HashMap or something? At any rate, it''s very very strange to have an object which can be equal to objects of other types, but never to an object of the same type.

Share this post


Link to post
Share on other sites
They''re not keyed, although maybe they should be. At any rate, despite the fact that it might be a little ackward to have object equivalence with other types, semantically it works quite well. A texture''s primary identifying attribute is its name (in my case), so if you have a piece of code like textureSet.Contains(textureName), it reads "Does this texture set contain this texture?" In some cases I think it''s OK. And if I wanted to, I could always add a case for Texture object in my Equals function. It''s just that I never compare two Texture types.

But I''d still like to know what the heck is up with ArrayList, even if I should change the design a bit

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!