Software and Technology Ramblings

September 25, 2008

Flex – ObjectUtil.Copy() tip

Filed under: Flex — Doug Hays @ 12:51 pm

I just ran into an interesting issue with ObjectUtil and its handy Copy function.  After performing a copy, I was attempting to cast the resulting object and the overall result was null:

oldField = new Field();
newField = ObjectUtil.copy(oldField) as Field;
//newField is null 

Thankfully, a quick trip to Google helped me find the solution 

I added this to my Field class:

public class Field {...}

and I was a step further.  My last hurdle was using the Copy function on subclasses of Field.  As you might guess, you need to define the RemoteClass metadata on every class that you intend to copy. After I did this, ObjectUtil was again on my list of favorite ActionScript classes.

The reason?  ObjectUtil.Copy() uses AMF to serialize and de-serialize the original object to create the copy.  If AMF doesn’t have specific knowledge of your class, it will not return an object that will cast properly.

Thanks to Darron Schall for this helpful tip.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress