06.21.07

Flash CS3 / Flex 2 AS3 Warning: #3596: Duplicate variable definition.

Posted in Flash, Flex, Loops, errors at 12:07 am by Curtis J. Morley

Warning: 3596: Duplicate variable definition.

Problem:
You are using the same variable and Flex/Flash doesn't like it.
Fix:
Change the name of one instance or unnest your loop.

Bad Code:

var myArray1:Array = ["a","b","c","d","e"];
var myArray2:Array = [myArray1];

//This first loop goes through the parent array
for (var i:int=0; i < myArray2.length; i++)
{
//this second loop iterates through the nested array
for (var i:int=0; i < myArray1.length; i++)
{
trace(myArray2[i][i]);
}
}

Good Code:

var myArray1:Array = ["a","b","c","d","e"];
var myArray2:Array = [myArray1];

//This first loop goes through the parent array
for (var i:int=0; i < myArray2.length; i++)
{
//this second loop iterates through the nested array
for (var j:int=0; j < myArray1.length; j++)
{
trace(myArray2[i][j]);
}
}

or

var keys:String = "Word";
for (var i:int=0; i < keys.length; i++)
{
//Code
}

for (i=0; i < keys.length; i++){
//More Code
}

Once Again - Happy Flashing

9 Comments »

  1. Aaron said,

    June 22, 2007 at 7:58 pm

    Not quite right.

    The duplicate variable definition warning in AS3 is a problem with AS3.

    Anyone would expect this to generate a duplicate variable definition:

    for (var i:int=0; i

  2. Curtis J. Morley said,

    June 23, 2007 at 12:07 am

    Aaron,

    Thanks for the comment. Not sure what you mean by “duplicate variable definition warning in AS3 is a problem with AS3.” Would love to find out what you mean.

    Curtis J. Morley

  3. ajnabi said,

    July 3, 2007 at 2:03 pm

    Yeah, I think There is a bug in AS3 which does not recognize that the scope of two (non-nested) for loops are different and so the following code still generates 3596 duplicate variable definition warning:

    for(var i:int=0; i

  4. David Smith said,

    August 14, 2007 at 11:17 pm

    I see this issue as well; It appears that (in fake code to try to avoid filters)

    for var i 1-100
    {
    }

    for var i 1-100
    {
    }

    will generate a duplicate definition warning

  5. Curtis J. Morley said,

    August 15, 2007 at 4:33 pm

    Anything within the same scope cannot be typed again so if you have a nested for loop then you will need to have

    function init () {
    for (var i:int = 0; i<3; i++ ) {
    trace(i);
    }
    for (var j:int = 0; j<3; j++ ) {
    trace(”j = “+j);
    }
    }
    You can also reset the variable for reuse. If you do this then make sure that you do not give it a datatype again. Flash already knows what type it is and if you try to assign another variable with the same name in the same scope then it will return AS3 error #3596.

    function init () {
    for (var i:int = 0; i<3; i++ ) {
    trace(i);
    }
    for (i = 0; i<3; i++ ) {
    trace(”#2 loop = “+i);
    }
    }

    When I say within the same scope what I mean is if you have a function that contains ‘var i’ then within that function you cannot have another ‘var i’, if you have a class variable ‘var i’ to use it throughout he class you don’t need to datatype it each time unless you want to scoped to a particular function.  This is for the same reason that you wouldn’t do this:

    for (var i:int = 0; var i:int<3; var i:int++ ) {

    or this

    function init (var i:int) {

    The scope is already defined or implicitly inferred and doesn’t need to be restated.
    Hope this helps.
    Curtis J. Morley

  6. Ryan said,

    June 18, 2008 at 6:44 am

    From my point of view, this IS a problem with AS3 (actually, maybe a “feature”).

    In other languages, *var i:int* would be scope within the enclosing *for* loop and not exist outside.

    And, in my experience, this “feature” of AS3 is a massive pain in the ass. Suppose you copy and paste a *for* loop in your code. Now, you have to replace every *i* with some other index. And, if you forget to replace one, you could end up getting null pointer exceptions. So, you go back to debug your code and, at first glance, it doesn’t “seem” like anything is wrong. But when you squint at the screen (after an hour or so), you realize that a lowercase “i” really looks similar to a lowercase “j” in your editor, so now you start cursing 3596 and wish you could write your code in a different language…

    Not that this happens to me :)

    Ryan

  7. Down Variable Scope « Actionscription said,

    June 19, 2008 at 6:46 pm

    [...] Curtis Morley explains it well, unfortunately in the last comment in the following link: CurtisMorley on Variable Scopes. [...]

  8. curtismorley.com » Changed Code examples on ActionScript Error 3596 said,

    June 26, 2008 at 2:45 pm

    [...] Based on comments I have changed the code example in ActionScript Error 3596. [...]

  9. Terry Allen said,

    October 5, 2008 at 7:59 am

    I just wanted to thank you for making this post. I do not remember getting this error when using Flex 2 so I assume it has been added as a feature for debugging purposes..

Leave a Comment