Hope to add some value to this.

The duplicate variable error is only due to the fact that String is a native
object (don't know the correct name for this) of Flash AS3.
So you don't have to use the new key word for it.
Like this would work OK:

var arObj:Array = [{},{}]
function test():void
{
       for each(var i:Object in arObj)
       {
            var id:MovieClip = new MovieClip()
                trace("1" ,id);
            id.name = "test" + Number(Math.random()).toString();
            trace("2" ,id);
       }

       trace(i);
}

-----Original Message-----
From: flashcoders-boun...@chattyfig.figleaf.com
[mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of allandt
bik-elliott (thefieldcomic.com)
Sent: woensdag 4 augustus 2010 12:12
To: Flash Coders List
Subject: Re: [Flashcoders] Question on variable init in a for loop

no that's correct but id should be declared outside of your loop - as3 will
actually give you a duplicate variable error for putting it inside the loop.

you can also refer to your iterator (i) after the loop is done

i'd rewrite your function like this

var arObj:Array = [{},{}]
function test():void
{
      var id:String;

       for each(var i:Object in arObj)
       {
               trace("1" ,id);
               id = "test" + Number(Math.random()).toString();
               trace("2" ,id);
       }

       trace(i);
}

best
a

On 4 August 2010 10:45, Jiri <jiriheitla...@googlemail.com> wrote:

> I have the following case:
>
> var l:Array = [{},{}]
> function test():void{
>        for each(var i:Object in l){
>                var id:String;// = null;
>                trace("1" ,id);
>                id = "test" + Math.random().toString();
>                trace("2" ,id);
>        }
> }
>
> I seems that 'id' is not resetted to null on every iteration as I would
> expect. I have to explicitly set the id to null myself! Does some know why
> this is, or are my expectations just wrong.
>
>
> Jiri
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Geen virus gevonden in het binnenkomende-bericht.
Gecontroleerd door AVG - www.avg.com 
Versie: 9.0.851 / Virusdatabase: 271.1.1/3049 - datum van uitgifte: 08/03/10
16:22:00

_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to