Skip to main content
GameDev.net gamedev.net
🔒 Locked

ArrayList Troubles :: C#

Started by Zipster May 5, 2004 at 5:07 PM 5 replies 600+ views
Original Post
Zipster
Zipster
In my application, I have a collection of objects that each have their own unique string and integer associated with them. I want to be able to identify the object either by its integer value or string value, so in my object class I have the following Equals override:
public override bool Equals(object obj)
{
	// Determine type

	if(obj is string)
	{
		return (this.name == (string)obj);
	}
	else if(obj is int)
	{
		return (this.code == (int)obj);
	}
	else
	{
		return false;
	}
}
I then have an ArrayList that contains a bunch of these objects. However, whenever I called the IndexOf method on my ArrayList, I find that it never invokes my overriden Equals, and thus always returns false. However, when I call Contains, it does use my overriden version. To be more specific, I''m calling IndexOf through the indexer of a wrapper collection class, like so:
public CustomObject this [object index]
{
	get
	{
		if(myObjects.IndexOf(index) == -1)
		{
			return null;
		}
		else
		{
			return (CustomObject)myObjects[ contentCodes.IndexOf(index) ];
		}
	}
}
This indexer always returns null, even if the item I''m looking for is indeed in the ArrayList (I''ve put a watch on the ArrayList, and it does contain what I''m looking for). The indexer is called like this:
ObjectCollection["uniquestring"];
// Or
ObjectCollection[uniqueinteger];
 
It was my thinking that the object would eventually propogate down to Equals, and there the type would be resolved and the correct result returns, but I breakpoint Equals and it''s never called. Should I resort to an old-fashioned search loop, or is there something I''m doing wrong?
Zipster
Zipster
Eh, I''m not really looking for the traditional key-value structure. What I have is an object that can be accessed with either of two keys, so for me to use a key-value structure I''d have to write some sort of adapter object that converts associated strings and integers into the same intermediary key which I use to key the SortedList.
Zipster
Zipster
Nevermind, I ended up replacing it with this:

foreach(CustomObject c in myObjects)
{
if(c.Equals(index))
return c;
}

return null;

I actually like that better
Zahlman
Zahlman
How about having two HashMaps: one integer->CustomObject and one string->CustomObject ?
noparity
noparity
Keep in mind that every time you call CustomObject.Equals(object obj) on a value type (int in your case) you will have boxing/unboxing overhead. This isn''t necessarily bad, but if you''re iterating over a fairly large collection of CustomObjects frequently and calling .Equals(custominteger) you''re going to unnecessarily inflate your Gen 0 heap size, which will end up making the GC work harder than necessary. I would suggest adding an .Equals(int i) overload to handle an int:

public bool Equals(int i)
{
return (this.code == i);
}

Of course, this doesn''t help if need to call .Equals polymorphicly, but it will certainly work in your last example.
afterburn
afterburn


Type typ = ((object)ary[Index]).GetType();

MessageBox.Show(typ.ToString());

Possibly???
--What are you nutz?I have nothing to say to your unevolved little brain. The more I say gives you more weapons to ask stupid questions.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.