var o:Object = {title: 'asad'};
var v:ImageItemVO = o as ImageItemVO;
var v:ImageItemVO = ImageItemVO(o); // throws an error
my ImageItemVO has a public var named title.
After this code runs the “var v” is null. Why?
Can somebody give me an example of how to make it work?
The
asoperator is used for casting an object from one type to another, but only works if the object can be casted that way. If it can’t it will give younull. The other way of casting (the way you do it on the last line), will instead give you an error if the object cannot be casted.In this case you don’t want to cast at all, casting doesn’t work that way. Instead, you probably want to do something like this:
or if there are more properties, and you don’t want to type them all by hand:
This code will copy all properties in
otov.