getDefinitionByName error: “Variable … is not defined”

I wrote this post mainly as a reminder for myself, but it might be useful for anybody dealing with the same issue. In ActionScript3 you can get a class by its name like this:

import flash.utils.getDefinitionByName;
...
var myClass = getDefinitionByName("MyClass") as Class;

When you only use the name of the class, you will get an error: “Variable … is not defined”, because the Flash Player can’t find it at run time. There are several ways to make it work.

1. You can import the class and create a dummy variable. The dummy is needed, otherwise the compiler won’t add the class.

import domain.project.MyClass;
...
private var _dummyClass:MyClass;

2. Instead of a variable you can also use the following notation.

import domain.project.MyClass;
...
MyClass;

3. You can also use the Frame metadata tag:

import domain.project.MyClass;
...
[Frame(extraClass="MyClass")]

All these solutions work, but you need to specify the classes you want to use up front.

4. Far more easy, elegant and flexible is to include the path of the class when calling getDefinitionByName. You won’t need to import anything and you don’t have to create dummy vars or use extra metadata tags.

var myClass = getDefinitionByName("domain.project.MyClass") as Class;

Tags: , ,

9 Responses to “getDefinitionByName error: “Variable … is not defined””

  1. katopz Says:

    nice note, thx!, btw it’s “domain.project.MyClassMyClass” or “domain.project.MyClass” ?

  2. Gert-Jan van der Wel Says:

    Ah yes, you are right! It should be “domain.project.MyClass”. I updated it right away. Thanks!

  3. Weekly Shared Items – 16. March, 2010 | TOXIN LABS - weblog of a german design student from wuerzburg Says:

    [...] getDefinitionByName error: “Variable … is not defined” [...]

  4. curtisch Says:

    var myClass = getDefinitionByName(“domain.project.MyClass”) as Class;

    What do domain & project stand for? Example?

    Thank you!

  5. Gert-Jan van der Wel Says:

    For example: flash.display.Sprite

  6. ericsoco Says:

    effing awesome. i wish i had known this three years ago. yay fully-qualified class names!

  7. ericsoco Says:

    ps. curtisch — those are packages. so in this example, MyClass is in package domain.project.MyClass:

    package domain.project.MyClass {
    public class MyClass () {
    // …
    }
    }

  8. ericsoco Says:

    oi, sorry for the numerous comments. but this doesn’t seem to work unless you have a reference to the class created via getDefinitionByName somewhere in the class that creates it. you don’t need an import statement, just a reference — this worked on my first try, because i had a cast to the getDefinitionByName-created class later in my code. commenting out that cast (and therefore the reference to the class) broke the getDefinitionByName functionality.

    any thoughts?

  9. Gert-Jan van der Wel Says:

    Hi ericsoco, sometime it doesn’t work out for me too and I can’t quite figure out why and how.

    First I compiled it with a reference in the code. Then I remove it and I was amazed to find out that everything still seemed to work. But I’m having trouble to use it in other classes… Is there such a thing as a compiler history?

Leave a Reply