# Using Proxy to foreach over a custom collection, using a Dictionary as storage

**URL:** <https://forum.kirupa.com/t/using-proxy-to-foreach-over-a-custom-collection-using-a-dictionary-as-storage/298297>\
**Category:** flash\
**Created:** [October 16, 2009, 5:34am UTC](https://forum.kirupa.com/t/using-proxy-to-foreach-over-a-custom-collection-using-a-dictionary-as-storage/298297 "2009-10-16T05:34:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![secoif](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@secoif](https://forum.kirupa.com/u/secoif)\
**Post date:** [October 16, 2009, 5:34am UTC](https://forum.kirupa.com/t/using-proxy-to-foreach-over-a-custom-collection-using-a-dictionary-as-storage/298297/1 "2009-10-16T05:34:14Z")

</div>

Hi.

So I want to be able to implement some kind of interface that allows me to abstract the specific implementation of the storage I’m using. I’m having it store ‘Pin’ Objects (which have a name property among other things)  
eg

```auto

public interface IPinMap
{
    function getPin(name:String):IPin
    function hasPin(name:String):Boolean
    function addPin(pin:IPin):void
    function removePin(pin:IPin):void
}

```

Now, my original implementation was simply extending _flash.utils.Dictionary_, but then I decided to make it more abstract/domain-specific by adding a few methods like hasPin etc (instead of using the \*hasProperty \*of _Dictionary_, which doesn’t make much sense in the domain of Pins) then this made me think I should abstract the whole ■■■■ thing and just use a private Dictionary member as the storage in case later I want to switch to using Vectors or something else (eg [as3ds HashTables](http://code.google.com/p/as3ds/) or something) as the storage medium.

and this is fine, but **I’ve come across a problem: Iterating over this set of data.** I’d prefer not to have to create a Javaesque Iterator object for this, as that is fairly unconventional in the actionscript world.

Ideally, I could loop by doing something like:

```auto

var pinMap:PinMap = new PinMap();

pinMap.addPin(new Pin("apple"));
pinMap.addPin(new Pin("orange"));
pinMap.addPin(new Pin("banana"));

for each (var pin:Pin in pinMap) {
     trace(pin.name);
}

```

Now I know [this is possible through extending [URL=“http://livedocs.adobe.com/flex/3/langref/flash/utils/Proxy.html”]Proxy ](http://www.kirupa.com/forum/showpost.php?p=1961524&postcount=236)and using it’s

\*\*nextName  
nextNameIndex  
nextValue  
\*\*  
functions.

but \*\*I have no idea how to go about implementing these on a [Dictionary](http://livedocs.adobe.com/flex/3/langref/flash/utils/Dictionary.html)\*\*object, because it doesn’t have a numeric index…

do I have to do something like:

[LIST=1]  
[_]use a foreach loop over the dictionary till i find the private static var \_currentElement (if none, return the first element returned by foreach)  
[_]move one foreach loop step forward to the next element  
[_]store that in \_currentElement  
[_]return \_currentElement  
[/LIST]

See the problem with this is that there’s a lot of thrashing going on when foreaching over the data:

EG **to loop over a set of length n, I have to do n! (factorial) loops** over the ■■■■ thing, which is clearly not very efficient.

Should I switch to [index-based storage](http://help.adobe.com/en_US/AS3LCR/Flash_10.0/Vector.html), or is there a way to get around this? Another solution?

Thanks.
