Hey all, long time fan, first time poster
Iām trying to create a function that determines which of 4 similar classes to create, and returns the new object itself.
Basically, I have a base class (Panel) that has 3 additional variations in layout, each of which has a subclass: TextPanel, FeaturePanel, and LinkPanel. The two parameters for my function are the ākindā of window to create and its name (ālinkStringā), both of which are provided by external XML. Iām wondering if itās even possible to return the new Panel as an objectā¦?
My code will probably explain this better than I can:
// create the new Panel variable ("addPanel") with a call to newPanel:
var addPanel = newPanel(linkList*.attribute("kind"), linkString);
// newPanel function:
public function newPanel( g:String, h:String ) {
switch ( g ) {
case "Text":
return new TextPanel(h);
break; //also, are these 'break's redundant?
case "Feature":
return new FeaturePanel(h);
break;
case "Link":
return new LinkPanel(h);
break;
default:
return new Panel(h);
}
}
within the Panel constructor, the provided string (linkString / āhā) is set as the Panelās .name. I did not copy that function into the subclasses, since they inherit it - unless Iām mistaken.
Iām new to AS3 and self-taught, so I realize this may be crazy talk, or just plain ugly coding. Hopefully this makes some sense though. Thanks in advance for any help!