if-statements – checking strings for being null

hello again!
Yesterday I wrote some code again and had to check if a String is null or not. The check was performed in a for-loop.
Below is some example code:

ActionScript
public var arrToFill:Array = [];
[... some more code here ...]
private function foo():void
{
  var i:uint;
  for( i = 0; i < 10; ++i)
  {
    if(!arrToFill[i])
    {
      arrToFill[i] = '';
    }
  }
}

This function was called different times and I wondered, that the if-statement returned false everytime.
For me it was theoretically correct that an Array-Element with a value of ” is not empty, because ” is an empty String for me, but it is something.
ActionScript3 doesn’t work like that, so for AS3 a String with a value of ” is equal to null or 0.

Conclusion:
If you have code like this and want to preallocate an element, just set it to 1. Then you can do a simpy

ActionScript
if(!arrToFill[i])

and everything would be fine.

Comments (0)

› No comments yet.

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Pingbacks (0)

› No pingbacks yet.