06.21.07

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

Posted in errors, Flash, Flex, Loops 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

16 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 the 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..

  10. curtismorley.com » AS3 Compiler Error# 1021: Duplicate function definition. said,

    October 22, 2008 at 11:30 pm

    […] AS3 Error 3596 […]

  11. curtismorley.com » Flex / Flash Compiler Error #1023: Incompatible override. said,

    October 23, 2008 at 2:04 pm

    […] AS3 Error 3596 AS3 Error 1021 AS3 Error 1023 (Different error same number) […]

  12. curtismorley.com » ActionScript Error 1184: Incompatible default value of type int where String is expected. said,

    October 23, 2008 at 3:04 pm

    […] AS3 Error 1047 […]

  13. science&code » AS3: What were they thinking? said,

    January 12, 2009 at 5:59 am

    […] sites for a solution (I was thinking of some kind of compiler-flag to use normal scopes) I found precious advices like this that told me that the above was “good code”… did I miss some kind of paradigm […]

  14. Sam said,

    April 15, 2009 at 4:21 pm

    In researching this issue I was fairly dumbfounded to find that the following code compiles, runs and evaluates to 99. As expected the first log entry is not written but the int variable is initialized which makes no sense whatsoever.

    if (1 == 0)
    {
    var test:int = 88;
    logger.info(“test ” + test.toString());
    }
    else
    {
    test = 99;
    logger.info(“test2 ” + test.toString());
    }

    I would love any explanation as to why this would ever be considered correct or desirable behavior.

  15. jason said,

    April 24, 2009 at 1:27 pm

    You can’t also get this error in a switch/case setup when using vars, like so:

    switch (someValue) ) {
    case A:
    var i:String;
    do something;
    break;

    case B:
    var i:String;
    do something;
    break;
    }

    Will throw it. I ignore them in this case. Thoughts?

  16. Dave said,

    March 9, 2010 at 5:00 am

    Once again shows that actionscript is a real scripting language and not really a structured language.

    I disable this specific warning by adding the following command line parameter to the compiler:

    -compiler.warn-duplicate-variable-def=false

Leave a Comment