Actually, this is by design, as of AS3.
The compiler has detected a duplicate definition for a variable. This
can lead to unexpected results. ActionScript does not support block
level scoping of variables. All variables defined within a function body
exist within the same scope, even if they are defined within an if
statement, while statement, for statement, and so on: for example, the
following code redeclares the variable x twice:
function test() {
var x:Number = 10;
if (true) {
for (var x=0; x < 5; x++) // warning here, this is the
second defintion of x
trace(x);
}
trace(x); // 5, not 10. The last value set by the for loop
above is the current value of x
}
Tracy
________________________________
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Seth Caldwell
Sent: Wednesday, September 26, 2007 1:35 PM
To: [email protected]
Subject: RE: [flexcoders] Very strange runtime behavior - if logic
doesn't work?????
This might be a compiler optimization issue. Try putting something in
the if statement that actually matters instead of just a variable
declaration that is never used...
Like a global variable, for example:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
<![CDATA[
[Bindable]
var g:Number = 0;
public function testit(myBool:Boolean):void
{
if(myBool)
{
g++;
}
}
]]>
</mx:Script>
<mx:Button x="273" y="345" label="{g}"
click="testit(false)"/>
</mx:Application>
It stays 0 when I click the button...
How are you calling your crazy function?
Seth
________________________________
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of dbronk
Sent: Wednesday, September 26, 2007 10:10 AM
To: [email protected]
Subject: [flexcoders] Very strange runtime behavior - if logic doesn't
work?????
Very strange...
I had an apparent bug in my code and simply could not find it so I
created a very basic function to help test. Here is the code:
public function crazy(myBool:Boolean) : void
{
if ( myBool )
{
var s:String;
}
}
Yes, it does nothing. I place a break point on the var s:String; line
to test the logic of the if statement. When I execute, it does not
matter if I send in a true or a false to this method, it ALWAYS
evaluates the expression to true, even though I can see in the
debugger myBool is false. I tried:
if ( myBool == true )
To see if that had any luck. It did not. After scratching my head a
bit I tried:
public function crazy(myBool:Boolean) : void
{
if ( myBool )
{
var s:String;
}
else
{
var ss:String;
}
}
Viola!!! Adding the else and the logic now works fine. Is it a
requirement that I must have an else? If so, why?
Thanks,
Dale