AS3 Get the current class name from a static method - actionscript-3

i have to read the current class name inside a static method. For non-static method it's easy i just call getQualifiedClassName(this) but inside a static method this is off course invalid. Any idea ?
Thanks

You can use getQualifiedClassName(prototype.constructor)
in the class' static method

You don't have a direct way of doing this. If is just inside one class that you need this, you can try to add a static member holding a reference to the class.
static private const CLASS:Object = YourReflectedClass;
then, just use that in your static method:
protected static function doReflection(): void {
var className:String = getQualifiedClassName(CLASS);
}
It would be helpful to know more about the use and the setup.

Related

Call to a possibly undefined method '' through a reference with static type Class

I made small .fla file in Flash Professional, and I have added .as (ActionScript File) in Flash Professional, and I have added something like code below to .as (ActionScript file), but the error appears and I am trying to figure it out, but can't, so I decided to post it in here instead.
package
{
import flash.display.MovieClip;
public class Bag extends MovieClip
{
static var firstBag:String;
public static function set setFirstBag(value:String):void
{
firstBag = value;
}
public static function get getFirstBag():String
{
return firstBag;
}
}
}
and I called it like this:
button1.addEventListener(MouseEvent.CLICK, onClickFirstButton);
function onClickFirstButton(e:MouseEvent):void
{
Bag.setFirstBag("First slot in the bag has been filled up!");
}
But I have received this following error:
Call to a possibly undefined method setFirstBag through a reference
with static type Class.
What could I do wrong?
The .as file and .fla file are on the same folder.
if I changed the Bag class to static. The error will be like this:
The static attribute may be used only on definitions inside a class.
Your answer much appreciated!
Thank you!
You're useing get like it is a mettod, but thay are accessors for properties so intead of:
Bag.setFirstBag("First slot in the bag has been filled up!");
use
Bag.setFirstBag ="First slot in the bag has been filled up!";
A few additional thoughts...
While syntactically valid, the definition and naming of your getter and setter is confusing and atypical, which I think contributed to your confusion about the behavior. You've actually defined two separate properties, one is write-only ("setFirstBag") and one is read-only ("getFirstBag"). Usually you define a getter/setter as the same property (ex "firstBag"), and without any "get" or "set" in the property name, since that is what the getter/setter is defining for you. Example:
private static var _firstBag:String;
public static function get firstBag():String {
return _firstBag:
}
public static function set firstBag(value:String):void {
_firstBag = value;
}
// usage
Bag.firstBag = "stuff";
trace(Bag.firstBag); // "stuff"
Also, you may very well have a good reason to use a getter/setter here, or you might just prefer it, but from the code you posted you could just define a public static var to do the same thing. (If you did, refactoring into a getter/setter if you needed some side-effect logic would be trivial, since the public API remains the same.)

conceptual issue in inheritence property of actionscript-3

say, Child class is inheriting Father class and Father class is inheriting spark TextArea class. now from an mxml file (in FLEX4), i am creating multiple objects of Child class. Father class have few static attributes whose values are set by private methods, calling from constructor. Now the question is: all these static attributes are set every time while Child class objects are being created one by one?
If answer is yes then Is it possible that Father class static attributes are set only once and not depends upon the number of Child class objects creation.
Please provide any suggestion or tips
Thanks in advance.
If you are setting static variables from an object's constructor or methods called from the constructor, then yes, they will be set every time. In order to prevent that, just check whether the variable is already set.
public class Foo {
public static var bar:Object;
public Foo(value:Object) {
if (!bar) {
bar = value;
}
}
}
First decide if those static members are really all that important to store as statics because statics are associated with a Class and not an instance it's usually a signal that you're probably doing something you shouldn't if instances are modifying or reading static members. You probably should use a factory method if you need to share that information with the instances. However, if you're sure you should do it then you can use a static initializer block to initialize the members when the class is loaded. Downside is that block throws an exception it can be hard to track down:
public class SomeObject {
private const _someStaticMember : String;
private const _someOtherStaticMember : SomeOtherObject;
static {
_someStaticMember = "foobar";
_someOtherStaticMember = new SomeOtherObject();
}
}

What values are safe to use to initialize class definition's static and const members?

Phrased differently, this question could read, "What is the order of compile-time variable declarations and definitions?"
I can't recall specific examples at the moment, but I know I've run into trouble when initializing const and static values in my class definitions due to the declaration of those values occurring out of order.
I know I can instantiate objects when declaring static const members, like so:
public class ConstsWithNewObjects {
public static const DEFINED_NOW_2:Object = {something:"Defined!"};
public static const DEFINED_NOW_3:Object = new CustomObject("Defined!");
}
But, if I'm accessing one of those members from another static or const value, I imagine race conditions arising, like so:
public class ConstsWithOtherConsts {
public static const DEFINED_NOW_1:Object = DEFINED_NOW_3; // Does this exist, yet?
}
ActoinScript Hero Jack Dunstan has covered this exact topic in great detail on his blog: Class Bootup Part 2.
Regardless of Jack's findings; I would recommend, for the sanity of your readers; that you keep your static initialiser code as clean, and simple as possible - don't forget that static fields can call static methods; you can also make use of a static Class initialiser method as well.

does static methods need to use static properties?

I am using as3. Just a simple question.
If I created a static method. say I decide to call on other methods within that static method. Do those methods I call on need to be static as well? what If I used some of the properties. Not to store data permanently, but just within that process. Do those properties need to be static ??
Yes. If anything is called static that means that it is related not to the current instance of the class but to the whole class therefore it must act instance-independent, e.g. use other static fields and methods if needed.
No, they do not. You can use any type of variable/method from within a static method, including, of course, local variables. However, there is no concept of "this" in a static method, since the method is not being executed on an instance, but on the class itself. Therefor, the following (inside a class declaration) is illegal:
public var myInstanceVariable : int;
public static function myStaticMethod() : void
{
// Illegal:
myInstanceVariable = 1;
// Illegal (same as above, "this" is implicit):
this.myInstanceVariebl = 1;
// This however is legal (local variable):
var localVal : int = 1;
}
The references to myInstanceVariable above are illegal because that variable is an instance variable, which is not accessible to the static method. Because static methods are not executed on an instance in the first place, the "this" special variable is not valid.
If you wanted to, you could keep a static reference to an instance, and execute methods on said instance. That's the key idea behind the common singleton pattern.
private static var _INSTANCE : MyClass;
public static function myStaticFunction() : void
{
_INSTANCE.doSomething();
}
Here, the _INSTANCE variable can be referenced from the static method, because the variable itself is declared as static.
To avoid confusion, there are actually answers to both the questions you asked:
Do method calls within a static method need to be static? Li0liQ answered this.
Do variables used within a static method need to be static? richardolsson answered this.
To summarize, within a static method, you can only access static variables and methods EXCEPT if you define local variables within the scope of the static method.
private var instanceVar : MyClass;
private static var staticVar : MyClass;
public static function myStaticFunction() : void
{
// Illegal, instance variable
instanceVar = new MyClass( 1 );
// Illegal, method on instance variable
instanceVar.someMethod();
// Legal, scoped local variable
localVar : MyClass = new MyClass( 1 );
// Legal, method on scoped local variable
localVar.someMethod();
// Legal, static variable
staticVar = new MyClass ( 1 );
// Legal, method on static variable
staticVar.someMethod();
}
It makes sense if you think about it a little, but it's not an entirely clear concept at first.

Friend methods/classes for AS3 packageless classes

Hi I'm wondering if I can have a packageless () AS3 class call a private method on the main class in the file. For example:
package demo
{
public class MyDemoClass
{
var helper:FriendlyHelperClass = new FriendlyHelperClass(this)
}
private function methodToCall():void
{
...
}
}
public class FriendlyHelperClass
{
public function FriendlyHelperClass(demo:MyDemoClass)
{
demo.methodToCall()
}
}
The call to methodToCall() from FriendlyHelperClass will fail as it is a private member of the MyDemoClass. Is there any way to call the methodToCall() method from the FriendlyHelperClass without extending MyDemoClass.
Basically I'm looking for inner class functionality that Java has or some sort of C++ style friend class.
Short answer : no.
You can never access a private member from outside a class in ActionScript. What you could do is use a namespace instead of a private scope. This would allow to give access to some members to selected classes. This is the closest of a friend class that you will get in AS3.
I'm afraid that is not possible, but if you make the class dynamic, then you can edit it while the program is running, and possibly add some useful functions to it, to access the private functions. I haven't tried it though.
Dynamic classes
Without testing the code, and knowing what your full problem. you can try passing the functions you need into the embedded class as a callback. e.g.,
helper.methodToCallCallback = this.methodToCall;
then inside FriendlyHelperClass:
this.methodToCallCallback();