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
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
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?
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.
Pingback: science&code » AS3: What were they thinking?
Pingback: curtismorley.com » ActionScript Error 1184: Incompatible default value of type int where String is expected.
Pingback: curtismorley.com » Flex / Flash Compiler Error #1023: Incompatible override.
Pingback: curtismorley.com » AS3 Compiler Error# 1021: Duplicate function definition.
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..
Pingback: curtismorley.com » Changed Code examples on ActionScript Error 3596
Pingback: Down Variable Scope « Actionscription
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
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
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
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
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
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