PDA

View Full Version : Advanced: Accessing a class's static functions from a variable of type Class



lovepuppy
February 19th, 2010, 11:40 AM
I realize this is kind of an odd issue, but I am wondering if there's any way to get Flash to allow me to access a class's static functions using a class variable that points to the class. Example:

I create a class called FooClass that has a static function named foo

I then create a variable of type Class that points to it

var class:Class = Class(getDefinitionByName("FooClass"));

However, when I try to call foo() using the variable, it errors saying the function doesn't exist, even though the class has the function.


class.foo();

It is entirely possible that I have misunderstood what the Class variable type does in Flash, if that is the case, then I'll rephrase.

I have an array of classes. That array stores an ID and name for each class as well as storing a Class variable that points back to the class. I want to be able to grab an arbitrary element from that array and call a static function based on the class of the element I selected.

Any thoughts are appreciated!

mykhel0003
February 19th, 2010, 12:05 PM
hi,
here's simple code that might help:

FooClass.foo();

this will call the static function "foo" function in FooClass...

if you have other static variables inside the FooClass, you can call them:
FooClass.var1
FooClass.var2

goodluck :D

BoppreH
February 19th, 2010, 12:08 PM
It works here:

var _class:Class = Class(getDefinitionByName('flash.events::Event'));
trace(_class.ENTER_FRAME)

Are you sure the "foo" property is set to PUBLIC?

lovepuppy
February 19th, 2010, 12:36 PM
sigh. It's always the stupid little things that get you. Thanks for shaking my brain loose Boppreh.

santiagopuentep
April 30th, 2010, 03:10 AM
var className:Class = getDefinitionByName("Math");
// works fine with a public static variable
className.PI
// throws compile time error with function
className.random()
// doesn't throw error anymore
Class(className).random()

Does anyone know why a variable of type Class has to be casted as Class to compile right??? Weird.

sFoster83
April 30th, 2010, 10:09 AM
looks like a bug to me... the class object has some weird behavior.
This doesn't work either:

package {
class {
public var someInstance : instanceType;

public function get instanceType() : Class {
return Event;
}
}
}

I don't think any of it was intentional.. just not used commonly enough.
Probably has a bug report somewhere, and if not then there should be

IQAndreas
April 30th, 2010, 11:03 AM
Does this work?
className.random.call();