This content has been marked as final. Show 7 replies
There are many ways to do this. It will depend on what you plan to do with it.
Array is very simple, but has limitations, "associative array"(object) has benefits, such as rapid random access. Use a collection if you need binding, or a Dictionary if you need object keys, etc.
I plan to keep a list or array of objects. The objects are of the same class type. How would you suggest I implement something like this? Should I just use a dictionary and key off some property of my objects? Does flex use a hashcode or override a equals method...something like java?
An object itself can be used as a Key in the Dictionary.
You can do dictionary[ object ] = object;
Yea I realize a dictionary can use the object as the key. However, that leaves me in the same problem as doing ArrayCollection.contains(object).
I guess what I need to know is how does Flex (or actionscript I suppose) handle object equality? In Java you override the equals and hashCode methods.
I don't get what you mean by same problem as doing ArrayCollection.contains?
if (dictionary[object] == null)
object is not in dictionary.
object is present in the dictionary.
Internally Flash/VM can be employing some hash code or object id to get this to work.
I don't understand why you need to understand how AS handles Object equality either as it is already handled by the VM without any coding requirement from the users.
I was just curious how it is done. In the other languages I develop in (Java, C#, Python) I can control how equality is handled for my own objects. So, if I have a Person class perhaps I want two Person objects to be considered equal if they both have the same ID or Name...whatever I like.
I'll keep looking because I imagine there is a way to do this.
Looks like you're right, you can't override the equality operation in Flex. You can try and use ObjectUtil.compare(Object, Object).
But it looks like you can implement the valueOf method and get behavior similar to what I am looking for.