Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Flex's ArrayCollection's Contain method look at memory reference?

When using .contains() on an ArrayCollection in Flex, it will always look at the memory reference. It does not appear to look at an .equals() method or .toString() method or anything overridable. Instead, I need to loop through the ArrayCollection every time and check each individual item until I find what I'm looking for.

Does anyone know why Flex/ActionScript was made this way? Why not provide a way from people to use the contains() method the way they want?

like image 569
DyreSchlock Avatar asked Dec 23 '08 19:12

DyreSchlock


2 Answers

Couldn't you just extend ArrayCollection and override the contains() method? Alternatively you can paste the source for ArrayCollection into an "mx/collections" package in your project and modify the source; this "monkey-patching technique" will override the behavior throughout your entire project. However I would be extremely cautious about changing ArrayCollection in that manner: since it's used all over the place in the Flex APIs there is a good chance you'll start breaking other components in the framework.

like image 146
cliff.meyers Avatar answered Sep 24 '22 16:09

cliff.meyers


The contains() method searches by reference, correct (I believe even for primitives), so if you're trying to find a string or an int in an ArrayCollection, you'll have to do the searching yourself, by some variation of looping or searching. I don't think any of us could tell you why there isn't, say, an optional parameter on that method indicating whether to search by ref or by val, though; so it goes, as they say.

But I'd definitely warn you off monkey-patching the framework code -- that's just asking for trouble. :)

like image 37
Christian Nunciato Avatar answered Sep 23 '22 16:09

Christian Nunciato