getDefinitionByName() produces ReferenceError: Error #1065


Something we often do here is get the “id” of a clicked menu button and instantiate a section of a site, depending on the ID passed. Usually all sections of a site inherit from a generic “Section” class, then have additional specific capabilities depending on the section. So, depending on the ID passed, we need to create an instance of the particular class.

This means we need to get the class name dynamically from the string passed by the button.

Let’s imagine the “music” button was pressed. We’ll either have a listener for this or we’ll directly call a method called loadSection(), passing the ID (the name of the button usually). We name instances in camel notation, so that’ll be “music”.

This string goes over to the loadSection() method and in here we use a custom StringUtils class to convert the first letter to upper case, getting us the required class name as a string. So this gives us “Music”. A string is no good on its own though – we need to get the class reference for this, as follows:

// required "dummy" ref to ensure class is compiled...
var dummyRef:Music;
// get class reference
var classRef = getDefinitionByName(id);
// instantiate class instance as current section
section = new classRef();


All well and good. The trouble is sometimes it just doesn’t work and you’ll get:

ReferenceError: Error #1065: Variable Music is not defined

There’s a thread here where the person was getting this, and it drifted off into another debate without being solved. The import statement is there. The dummy class reference is there, but still it doesn’t work.


What is not immediately clear is that you need to provide a fully qualified class path to getDefinitionByName() – even though you have set up an import.

Amend as follows (for example) and your problem is solved:

// var classRef = getDefinitionByName(id);
var classRef = getDefinitionByName("sections." + id);

“sections.” here is a reference to our package structure.

We hope this helps you.

Cheap web hosting

Comments on this post

  1. JAB says:

    Yeah ! You just saved my life…. 🙂

    • Tomas Sancio says:

      Mine too. I had written

      var ac:Object = getDefinitionByName(“ArrayCollection”);

      and it wouldn’t work. So I tried

      var ac:Object = getDefinitionByName(“mx.collections.ArrayCollection”);

      and it worked. Thank you very much!

  2. Orland Media says:

    Great. 🙂

  3. Andy says:

    Is there no way to accomplish this without the dummy var?

    • Orland Media says:

      Hi Andy. As far as we know there is no way to avoid the need for this dummy var. Without it you get the reference error again.

      Having to list all the possible types with dummy vars quite seriously detracts from the dynamic nature of this otherwise useful approach. It almost makes it worth just using a switch statement and instantiating the different possible class instances longhand instead . Therefore if anyone has anything to add please feel free.

  4. This is solved by selecting “Export Frame 1” in the properties of movieclip or sound!!! (In the library).

  5. The need to declare a ridiculous dummy var really wastes the power of getDefinitionByName. Importing the class in the project root should be enough

  6. Ivan Milosavljevic says:

    This solution is not good if you use String, int, ArrayCollection etc … since it will try to seek sections.String and so on …

Add a comment: