6 Replies Latest reply on Jun 23, 2010 1:17 PM by Francisc

    Strange problem with casting

    Francisc Level 3



      Bindable variables that are set by an ResultEvent don't work if cast like this:


      myVar=event.result as uint   - OR -   myVar=event.result as ArrayCollection


      However it will work like this:


      myVar=uint(event.result)   - OR -   myVar=new ArrayCollection(event.result)


      Any idea why this is so?

        • 1. Re: Strange problem with casting
          rtalton Level 4

          That is weird. I use your first example (myVar=event.result as ArrayCollection) all the time with no problem. I am returning strongly-typed data from .NET, so maybe Flex can convert it in a simpler way since the data types match the Flex data types.

          • 2. Re: Strange problem with casting
            Francisc Level 3

            I've never had this problem in Flex 3. I've always used that.


            I am using AMFPHP and in theory return Array which is by default converted to Object by the ResultEvent.

            • 3. Re: Strange problem with casting
              msakrejda Level 4

              Well, you're playing kinda fast and loose with the term "casting".


              var foo:ArrayCollection  = new ArrayCollection(event.result)


              is not a cast: it builds a new ArrayCollection with the result. If the runtime type of event.result is Array, this will work fine. However, if you just cast that to ArrayCollection (using the 'as' operator), you'll get null, since an Array is not the same type of object as an ArrayCollection.


              I'm not sure what's going on in the uint case; it could be similar.


              And the return is not "converted" to Object: it's just declared as Object because the framework has no compile-time information about the actual runtime type of the result.

              1 person found this helpful
              • 4. Re: Strange problem with casting
                Francisc Level 3

                Yeah, cast isn't appropriate for the ArrayCollection(xxx) version.


                And this won't work: var foo:ArrayCollection  = new ArrayCollection(event.result)

                Unless you add as Array to the end as the event.result is always an object: var foo:ArrayCollection  = new ArrayCollection(event.result as Array)


                Problem is I used to do this in Flex 3 and it worked:


                var foo:ArrayCollection=event.result as ArrayCollection; (which is a genuine cast)

                • 5. Re: Strange problem with casting
                  msakrejda Level 4

                  You're right, the 'as Array' is required. It doesn't change the runtime type of event.result, but it does tell the compiler, "Hey, I know what I'm doing. I know the type is declared as Object, but it's really going to be an Array in there, so why don't you treat it as an Array when compiling." Since the ArrayCollection constructor expects an Array, that is necessary.


                  Re: the Flex 3 / Flex 4 change--perhaps things are no longer automagically wrapped in a collection? In that case, it's also not really a casting issue: just that the behavior of the deserializaiton mechanism has changed.

                  1 person found this helpful
                  • 6. Re: Strange problem with casting
                    Francisc Level 3

                    I see. What is the `best practice` for doing this thought?