Troublesome if statement

Hi everyone, have an issue which is think is the fault of an if statement. I’ve written this piece of code which i hope will dynamically change the height of a text box depending on how many characters are used in an external XML file. So the longer the string the bigger my box will get.

However on running this the text box always seems to come out with a height of “20”. My string is over 220 characters so should be “50” in length. I’ve posted the full code but believe the problem lies in the highlighted blue text below.

Does anyone know if this is possible to do or is there simply a prob with the code?

var xml:XML;
var txtFld:TextField = new TextField();
var DynHeight:Number;
var DynLength:Number;

var urlRequest:URLRequest = new URLRequest(“text.xml”);
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete, false, 0,
true);
urlLoader.load(urlRequest);

function urlLoader_complete(evt:Event):void {
xml = new XML(evt.target.data);
txtFld.text = xml;
DynLength = txtFld.text.length;
trace(DynLength);
}
function addTextField():void {
[COLOR=Blue] if (DynLength > 104) {
txtFld.height = 50;
trace(“Height 50”);
} else {
txtFld.height = 20;
trace(“Height 20”);
}[/COLOR]
txtFld.x = txtFld.y = 20;
txtFld.width = 510;
txtFld.multiline = true;
txtFld.wordWrap = true;
}
addTextField();
addChild(txtFld);

the addTextField function is being called before the urlLoader_complete function, at which point DynLength is not defined, so it is setting the height to 20.

Would a warning not pop up saying undefined variable? Any ideas How would I change the code to fix this?

A Number can not undefined, it’s null. So no, a warning wouldn’t pop up. You can fix it by putting the call “addTextField()” at the end of the “urlLoader_complete” function.

Perfect!! Thanks a lots