4 Replies Latest reply on Mar 18, 2008 2:05 PM by peterent

    Explicit casting

    AmrKamel
      Hello,

      I have question, there are two ways for doing explicit casting in AS 3.0:
      1- DataTypeIWantToConvertTo(Object I want to convert);
      2- ObjectIWantToConvert as DataTypeIWantToConvertTo;

      right?? So I expect there result to be the same!! cause today I was casting data from URLLoader object to XML object and only this works:
      var xmlData:XML = XML(_urlLoader.data);

      but when I try to do it the other way:
      var xmlData:XML = _urlLoader.data as XML;

      it gives me the following exception:
      TypeError: Error #1009: Cannot access a property or method of a null object reference.

      ??? So can you help me in this and make me understand more what is going on :)

      Thanks in advance :)
        • 1. Re: Explicit casting
          Gregory Lafrance Level 6
          I was hoping creating the XML object first would solve it but it did not:

          <?xml version="1.0"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">
          <mx:Script>
          <![CDATA[
          private var request:URLRequest = new URLRequest("states.xml");
          private var loader:URLLoader = new URLLoader();

          public function init():void {
          loader.addEventListener(Event.COMPLETE, completeHandler);
          loader.load(request);
          }

          private function completeHandler(event:Event):void{
          var xmlData:XML = XML(loader.data);
          trace("Output One:\n");
          trace(xmlData.toXMLString());
          var xmlData2:XML = new XML();
          xmlData2 = (loader.data as XML);
          trace("Output Two:\n");
          trace(xmlData2.toXMLString());
          }

          ]]>
          </mx:Script>
          </mx:Application>

          • 2. Re: Explicit casting
            AmrKamel Level 1
            Thanks Greg,

            So is this considered a bug?? or what?

            Thanks again :)
            • 3. Re: Explicit casting
              ntsiii Level 3
              In fact, event though it appears to be a casting, XML() is not. It is actually a "top-level" function. It is actually more flexible, and will attempt to convert the argument to XML So it can take a string that looks like xml and return XML. That may be what is happening in your case. The "AS" operator is a true cast. So its argument must be an object reference that contains an instance of the actual data type you are attempting to cast to. Another difference is the way errors are handled by the two cases. I do not recall the exact details, but they are discussed in the docs.

              Tracy
              • 4. Re: Explicit casting
                peterent Level 2
                Using the datatype(object) casting is confusing in several instances:

                var a:Array = Array(object); This doesn't cast object to Array - it creates an Array of 1 element.
                var x:XML = XML(object); This doesn't cast either - it creates an XML object out of its argument. This may be null if object or object.toString() doesn't return parseable XML. The object is already XML, then it does a cast.

                Using the AS operator is best:

                var a:Array = object as Array;
                var x:XML = object as XML;

                If datatype(object) cannot do the cast it will throw an exception; object as datatype simply returns null if the cast cannot be made.