Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9123249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:17:36+00:00 2026-06-17T06:17:36+00:00

here is my code line : new (getDefinitionByName(String( mypackage.MyDynamicClass )) as Class) ; This

  • 0

here is my code line :

  new (getDefinitionByName(String( "mypackage.MyDynamicClass"  )) as Class) ;

This generates an error : mypackage.MyDynamicClass is not defined.

I googled and found a solution : Use the name of the class in the import statement.

So,

  import mypackage.MyDynamicClass 
  new (getDefinitionByName(String( "mypackage.MyDynamicClass"  )) as Class) ;

It worked!!!

but, I am not satisfied with this solution, as it really violates the benefit of a dynamic class. If i know the name of the class ( in import ), then why i would be using it as a string ?

Any alternatives to make dynamic loading of classes work ?

Vishwas

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:17:37+00:00Added an answer on June 17, 2026 at 6:17 am

    I suggest that you use a abstract factory
    http://en.wikipedia.org/wiki/Abstract_factory_pattern
    Its a very flexible way to create objects without knowing wich class you are going to instantiate.

    Is in this abstract factory class in wich you will need to import all the clases you might create objects from, this will not require you to import all clases in your main class but you will still need to import the abstract factory and the interface to communicate with your new objects.

    Here is a quick example:

    /*
    The interface you will use to communicate with your objects
    */
    InterfaceForAllMyClasses.as
    public interface InterfaceForAllMyClasses
    {
        public function callMe();
        public function callMe2();
    }
    
    /*
    Your Classes wich implement the interface
    */
    Class1.as
    import mypackage.InterfaceForAllMyClasses;
    public class Class1 implements InterfaceForAllMyClasses
    {
        public function callMe() { trace("called Class1"); }
        public function callMe2() { trace("called Class1 too"); }
    }
    
    Class2.as
    import mypackage.InterfaceForAllMyClasses;
    public class Class1 implements InterfaceForAllMyClasses
    {
        public function callMe() { trace("called Class2"); }
        public function callMe2() { trace("called Class2 too"); }
    }
    
    /*
    The Abstract Factory
    */
    AbstractFactory.as
    import mypackage.InterfaceForAllMyClasses;
    public class AbstractFactory
    {
        public function giveMeObject(classNumber:Number):InterfaceForAllMyClasses
        {
            switch(classNumber)
            {
                case 0: return(new Class1()); break;
                case 1: return(new Class2()); break;
                // for any new class that you add you must add a case entry here
            }
        }
    }
    
    /*
    Your Program
    */
    import mypackage.InterfaceForAllMyClasses;
    import mypackage.AbstractFactory;
    MyProgram.as
    public class MyProgram
    {
        var abstractFactory:AbstractFactory = new AbstractFactory();
    
        public function main()
        {
            var x:InterfaceForAllMyClasses=AbstractFactory.giveMeObject(0);
            var y:InterfaceForAllMyClasses=AbstractFactory.giveMeObject(1);
    
            x.callMe();
            x.callMe2();
            y.callMe();
            y.callMe2();        
        }
    }
    

    If you cant import the classes from your main application because they are declared in external modules (swfs), then you can make each module as an Abstract Factory, here is an example:

    Interfaces/IModuleInterface.as
    
    package Interfaces
        {
            public interface IModuleInterface
            {
                function giveMeObject(classNumber:Number):IObjectInterface;
            }
        }
    
    Interfaces/IObjectInterface.as
    
    package Interfaces
        {
            public interface IObjectInterface
            {
                function callMe():void;
                function callMeToo():void;
            }
        }
    
    Modules/ModuleOne.mxml
    
        <?xml version="1.0" encoding="utf-8"?>
        <s:Module xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"         implements="Interfaces.IModuleInterface">
            <fx:Script>
                <![CDATA[
                    import Interfaces.IObjectInterface;
                    public function giveMeObject(classNumber:Number):IObjectInterface
                    {
                        switch(classNumber)
                        {
                            case 1:
                                trace("ModuleOne: Instantiating 1");
                                return(new ModuleOneClassOne()); 
                                break;
                            case 2:
                                trace("ModuleOne: Instantiating 2");
                                return(new ModuleOneClassTwo()); 
                                break;
                        }
                        return(null);
                    }
                ]]>
            </fx:Script>
            <fx:Declarations>
                <!-- Place non-visual elements (e.g., services, value objects) here -->
            </fx:Declarations>
            <s:Label x="10" y="10" text="Module One Loaded"/>
        </s:Module>
    
    Modules/ModuleOneClassOne.as
    
        package Modules
        {
            import Interfaces.IObjectInterface;
    
            public class ModuleOneClassOne implements IObjectInterface
            {
                public function ModuleOneClassOne()
                {
                    trace("ModuleOneClassOne: Instantiated");
                }
    
                public function callMe():void
                {
                    trace("ModuleOneClassOne: called callMe()");
                }
    
                public function callMeToo():void
                {
                    trace("ModuleOneClassOne: called callMeToo()");
                }
            }
        }
    
    Modules/ModuleOneClassTwo.as
    
        package Modules
        {
            import Interfaces.IObjectInterface;
    
            public class ModuleOneClassTwo implements IObjectInterface
            {
                public function ModuleOneClassTwo()
                {
                    trace("ModuleOneClassTwo: Instantiated");
                }
    
                public function callMe():void
                {
                    trace("ModuleOneClassTwo: called callMe()");
                }
    
                public function callMeToo():void
                {
                    trace("ModuleOneClassTwo: called callMeToo()");
                }
            }
        }
    
    AbstractFactoryInModules.mxml
    
        <?xml version="1.0" encoding="utf-8"?>
        <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                       xmlns:s="library://ns.adobe.com/flex/spark"
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       width="345" height="200">
    
            <fx:Script>
                <![CDATA[
                    import Interfaces.IModuleInterface;
                    import Interfaces.IObjectInterface;
    
                    import mx.events.ModuleEvent;
                    protected var module:IModuleInterface;
                    protected var object:IObjectInterface;
    
                    protected function ButtonLoadSwf_clickHandler(event:MouseEvent):void
                    {
                        loader.unloadModule();
                        loader.url=moduleUrl.text;
                    }
    
                    protected function loader_readyHandler(event:ModuleEvent):void
                    {
                        this.module = (loader.child) as IModuleInterface;
                    }
    
                    protected function ButtonCreateObject_clickHandler(event:MouseEvent):void
                    {
                        this.object = this.module.giveMeObject(Number(TClassNumber.text));
                    }
    
                    protected function BCallMe_clickHandler(event:MouseEvent):void
                    {
                        this.object.callMe();
                    }
    
                    protected function BCallMeToo_clickHandler(event:MouseEvent):void
                    {
                        this.object.callMeToo();
                    }
                ]]>
            </fx:Script>
    
            <fx:Declarations>
                <!-- Place non-visual elements (e.g., services, value objects) here -->
            </fx:Declarations>
            <s:Button id="ButtonLoadSwf" x="236" y="10" width="99" label="Load SWF"
                      click="ButtonLoadSwf_clickHandler(event)"/>
            <s:Button id="ButtonCreateObject" x="150" y="108" label="Create Object"
                      click="ButtonCreateObject_clickHandler(event)"/>
            <s:TextInput id="TClassNumber" x="96" y="107" width="46" text="1"/>
            <s:ModuleLoader x="10" y="39" width="325" height="60" id="loader" ready="loader_readyHandler(event)">
            </s:ModuleLoader>
            <s:TextInput id="moduleUrl" x="10" y="10" width="218" text="Modules/ModuleOne.swf"/>
            <s:Button id="BCallMe" x="96" y="137" width="150" label="callMe"
                      click="BCallMe_clickHandler(event)"/>
            <s:Button id="BCallMeToo" x="96" y="166" width="150" label="callMeToo"
                      click="BCallMeToo_clickHandler(event)"/>
        </s:WindowedApplication>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got this line of code here but its not working. private void Button_Click(object sender,
here is my current line of code: $(<li>).text($(this).text()).appendTo(#theList); Which results in <ul id=theList> <li>blah</li>
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get
Here is the code: string str; cin>>str; cout<<first input:<<str<<endl; getline(cin, str); cout<<line input:<<str<<endl; The
here is my code.... class line{ function db($host, $user, $pass, $db){ mysql_connect($host, $user, $pass)
Here is the code I have. I'm getting a red line under new MainFragment();
I've ripped out every single line of code here with the exception of this:
here is my code:(get file line num and word count) import System.IO import Data.Maybe
Here is my code that adds a line to the grid by pressing the
Here's my code. See the line that is commented out. When the element id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.