This unintuitive behavior isn't a choice the Flex team made... it's the way Ecmascript is required to work. The standards committee defining the next version of Ecmascript has been thinking about this issue. - Gordon
________________________________ From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of jer_ela Sent: Thursday, September 20, 2007 6:58 AM To: [email protected] Subject: [flexcoders] Re: Variable used before it's defined without compile errors Flex requires variables to be declared somewhere in a function, but doesn't care where it just rolls all variable definitions to the top of the function, so there is a phantom var val:String; at the top of the function. At the point where you call validateRequired val exists, but hasn't had a value assigned so it is null which is what you are seeing. No different than if you declared a variable without assigning a value in a language that required declaration at the top of the function. --- In [email protected] <mailto:flexcoders%40yahoogroups.com> , "Mika Kiljunen" <[EMAIL PROTECTED]> wrote: > > Hi! > I ran into this "weird" situation that as a programmer I think is a bug, but > I'm not sure if this is true in the flex world. See following code snippet, > especially how the variable "val" is defined (it is used at > "validateRequired" function call before it's even defined and Flex Builder > compiles it without a single error or warning, resulting on faulty > functionality since "val" gets used as value "null".) > > Is this correct behaviour that one should be beware of? > > .... > public function validateString( value:String,options:StringValidatorOptions, > language:ICommonLanguage ) : Array > { > var results:Array = new Array(); > // Validate required > var requiredError:ValidationError = validateRequired( val, > options, language ); > if ( requiredError ) > { > results.push( requiredError ); > return results; > } > > var val:String = ( value != null ? String(value) : "" ); > ..... > > Thanks, > Mika >

