Warning: 3596: Duplicate variable definition.

Message
Warning: 3596: Duplicate variable definition.

Adobe Documentation – link

NA

Curtis Morley – link

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:
[as]
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]);
}
}
[/as]
Good Code:
[as]
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]);
}
}
[/as]
or
[as]
var keys:String = “Word”;

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

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

8 thoughts on “Warning: 3596: Duplicate variable definition.

  1. 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

    Like

  2. 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

    Like

  3. 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

    Like

  4. 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

    Like

  5. 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

    Like

  6. 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

    Like

  7. To workaround the duplicate variable definition problem, you can write your loops like this:
    [as]
    var i:uint;
    for (i = 0; i < 10; i++) {
    // Stuff
    }
    for (i = 0; i < 10; i++) {
    // More stuff
    }
    [/as]

    As far as the best way to iterate over an Array, I have several things to say about that subject.

    myArray2 is a single element array, with just one element, an Array. If you’re trying to demonstrate multi-dimensional arrays, this isn’t the way to do it.
    If you are using a variable as your counter, have it as a uint because array.length is a uint and if you compare an int with a uint, casting has to happen
    Never have array.length in your maintenance condition. If it is, flash has to calculate the length of the Array on every single iteration.
    The “best” way to do an array isn’t the way it’s done here, read this to find out why: Iterating over an ActionScript Array

    Like

  8. There’s a problem with switches too:
    you get this error by doing this too:

    switch (suffix)
    {
    case “.jpg” :
    var img:ImageLoader = new ImageLoader(images[counter],{name:”loader” + counter,onComplete:completeHandler});
    trace(img);
    break;
    case “.swf” :
    var img:SWFLoader = new SWFLoader(images[counter],{name:”loader” + counter,onComplete:completeHandler});
    trace(swf);
    break;
    }

    This is strange because then I must call img.load (the two classes feature the same method name) and to to this I have to create a terrible workaround.

    Like

Leave a comment