views:

70

answers:

2

I have a strange problem regarding Flash error 1151: A conflict exists with definition i in namespace internal.

Here is the problematic code:

for(var i:Number=dt.getFullYear(); i >= dt.getFullYear()-90; i--)
{
    dtYear.addItem( {label:i, data:i} );
} //for

//-*-*-* Month
for(var i:Number=0; i < months.length; i++)
{
    dtMonth.addItem( {label:i, data:i} );
} //for

Or a more blatant example:

for(var i:Number=0; i < 12; i++)
{
    trace(i);
} //for

//-*-*-* Month
for(var i:Number=0; i < 12; i++)
{
} //for

Adobe gives an explanation:

You cannot declare more than one variable with the same identifier name within the same scope unless all such variables are declared to be of the same type. In ActionScript 3.0, different code blocks (such as those used in two for loops in the same function definition) are considered to be in the same scope.

What the friggin hell is this? I mean the i variable is all the time exists as a Number, typecasted as a Number, why the hell would the above code then fail?

If I modify it this way, it works, BUT THATS UGLY AND WHY IS THIS NEEDED? AAARGGGHHHH...Flash development makes me crazy. Gimme a gun :). Someone explain this one to me please.

Working code:

for(var i:Number=dt.getFullYear(); i >= dt.getFullYear()-90; i--)
{
    dtYear.addItem( {label:i, data:i} );
} //for

//-*-*-* Month
for(i=0; i < months.length; i++)
{
    dtMonth.addItem( {label:i, data:i} );
} //for
+3  A: 

This is called variable hoisting, in as3 there is no scope for the variable, the compiler will move all declared variable at the top of your function, so you can't declare twice the same variable in the same function.

Here the documentation about the variable usage and declaration for deeper information.

Patrick
Well then Flash/AS3 never was a user friendly language/UI :). What a stupid behaviour :(((. Thanks.
Jauzsika
+2  A: 

Short answer: you can't have "var i" declared twice in the same function. In your second loop, change "i" to "j" and you'll be gold.

Myk