# Troublesome if statement

**URL:** <https://forum.kirupa.com/t/troublesome-if-statement/262960>\
**Category:** Uncategorized\
**Created:** [June 11, 2008, 1:00pm UTC](https://forum.kirupa.com/t/troublesome-if-statement/262960 "2008-06-11T13:00:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ricmar](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@Ricmar](https://forum.kirupa.com/u/Ricmar)\
**Post date:** [June 11, 2008, 1:00pm UTC](https://forum.kirupa.com/t/troublesome-if-statement/262960/1 "2008-06-11T13:00:22Z")

</div>

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);

---

<div class="post-metadata">

**Author:** ![Iamthejuggler](https://avatars.discourse-cdn.com/v4/letter/i/ecc23a/32.png) [@Iamthejuggler](https://forum.kirupa.com/u/Iamthejuggler)\
**Post date:** [June 11, 2008, 1:10pm UTC](https://forum.kirupa.com/t/troublesome-if-statement/262960/2 "2008-06-11T13:10:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ricmar](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@Ricmar](https://forum.kirupa.com/u/Ricmar)\
**Post date:** [June 11, 2008, 1:33pm UTC](https://forum.kirupa.com/t/troublesome-if-statement/262960/3 "2008-06-11T13:33:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Iamthejuggler](https://avatars.discourse-cdn.com/v4/letter/i/ecc23a/32.png) [@Iamthejuggler](https://forum.kirupa.com/u/Iamthejuggler)\
**Post date:** [June 11, 2008, 1:45pm UTC](https://forum.kirupa.com/t/troublesome-if-statement/262960/4 "2008-06-11T13:45:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ricmar](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@Ricmar](https://forum.kirupa.com/u/Ricmar)\
**Post date:** [June 11, 2008, 1:58pm UTC](https://forum.kirupa.com/t/troublesome-if-statement/262960/5 "2008-06-11T13:58:53Z")

</div>

Perfect!! Thanks a lots
