3 Replies Latest reply on Jun 15, 2006 1:05 AM by davidv

    Casting Object to custom Class - Type coercion

    davidv
      Hi,

      I can't figure out how to do something as simple as cast an "Object" (which is get from an Array of object returned by mx:WebService call) to one of my own objects.

      This gives an error :

      testing.mxml


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:Script>
      <![CDATA[
      import test.Test;


      public function testme():void {
      var o:Object = new Object()
      o.p1 = "dsfqqsd";
      o.p2 = "fsdqfsd";

      var t:Test = Test(o);
      trace(t.p1);
      }
      ]]>
      </mx:Script>
      <mx:Button x="150" y="111" label="Test" click="{testme()}"/>
      </mx:Application>

      and the Test class :

      package test
      {
      public dynamic class Test
      {
      public var p1:String;
      public var p2:String;
      }
      }

      what I get by invoking the testme() function is
      TypeError: Error #1034: Type Coercion failed: cannot convert Object@111b4a1 to test.Test

      Anybody now what I'm doing wrong here ?

      thanks for your time,

      -david-
        • 1. Re: Casting Object to custom Class - Type coercion
          Level 7
          AFAIK you can only down cast (to a base class)
          --
          Jeckyl


          • 2. Re: Casting Object to custom Class - Type coercion
            inlineblue Level 1
            What you're trying to do in this example is not legal. "o" is an instance of Object, not an instance of Test. You can't arbitrarily cast an object to any class you want--the object must actually be an instance of the class (or a subclass).

            However, you mention webservice calls, so I'm assuming you're trying to get the webservice to return an instance of your custom class. AFAIK, this can't be done with the WebService tag--you must use RemoteObject in conjunction with FDS or ColdFusion as your server. In that case, you specify a mapping of your custom ActionScript class to a Java class on the server using the RemoteClass metadata tag. Here's some more info:

            http://livedocs.macromedia.com/labs/1/flex20beta3/00001293.html

            Otherwise, you cannot cast any object returned by a WebService call to a custom class. They're just plain Objects and you have to know the contents.
            • 3. Re: Casting Object to custom Class - Type coercion
              davidv Level 1
              Thanks to the both of you.

              It had already started to dawn on me in the meantime (there's nothing better for getting a grip on a problem than stepping away from the pc for a while:-) ). Coming from the Java world I should've known that you can never cast an object to something it doesn't extend.

              Inlineblue : You're right : I was trying to mimick the behavious of Flex Dataservices and remote Java objects but this time with a webservices interface (server side code is J2EE). It's much easier if the data in Flex is encapsulated in objects.

              Right now I'm thinking about creating a utility class that will 'copy' back and forth the data in the objects returned from webservices over to my custom classes in Flex/Actionscript. I don't know if it'll gain me anything in the long run but I just hate working with non-typed data.

              Thanks anyway !