casting between sibling classes, AS3

Posted by felix-gasca on Stack Overflow See other posts from Stack Overflow or by felix-gasca
Published on 2010-06-16T20:54:38Z Indexed on 2010/06/17 1:22 UTC
Read the original article Hit count: 243

Filed under:
|
|

I have two classes, derivedClassA and derivedClassB which both extend parentClass I'm declaring var o:parentClass and then, depending on what's going on, I want to cast o as either being of type derivedClassA or derivedClassB.

Essentially, this:

var o:parentClass  

...  

if(shouldUseA)  
    o = new derivedClassA();  
else  
    o = new derivedClassB();  
o.doSomething();

But it's not working, I'm getting all sorts of errors. Isn't this how class inheritance works? I feel like I'm missing something really fundamental, but I can't figure out what. Am I supposed to be using interfaces instead? Is there another way of doing what I want?

© Stack Overflow or respective owner

Related posts about actionscript-3

Related posts about casting