1
votes

Hey when I have an "Object" instance that doens't belong to any other class, how can I cast this to a class instance?

Example

A simple User class

public class User
{
    public var name:String;
}

When I try to cast

var obj:Object = new Object;
obj.name = "Alex";

var user:User = User(obj);

Flex gives me this error:

TypeError: Error #1034: Type Coercion failed: cannot convert Object@97a52b1 to models.User.

1

1 Answers

0
votes

Probably impossible, because User doesn't (automatically like in Java) extend Object class.

And if you would extend it, then the .name property would be lost through the type casting.

So you will have to create a new Object object and populate it "by hand".

   myObj = {};
   for (var key:String in myUser) 
             myObj[key] = myUser[key];