开发者

Get all static variables in a class

I have this ObjectType class which is a class to help me do something like this:

object.type = ObjectType.TWO

//ObjectType.as
package
{
    publi开发者_运维技巧c class ObjectType
    {
        public static var ONE:String = "one";
        public static var TWO:String = "two";
        public static var THREE:String = "three";

        public function ObjectType()
        {
        }
    }
}

Let's suppose I'm creating a new class and I need a property named type. In that property set function I want to make sure that it's value is one of the ObjectType variables. How can I achieve this?

public function set type(value:String):void 
{
    for (var o:Object in ObjectType) {
        if (value == o)
            this._type = value;     
        } else {
            //error
        }
    }
}


Not performance aware but without modifying anything you can use describeType function to check the static field and get the value back:

function valueInClass(clazz:Class, value:*):Boolean {
 return describeType(clazz).variable.(clazz[@name.toString()] == value).length() != 0
}

public function set type(value:String):void 
{
 if (valueInClass(ObjectType, value)) {
  this._type = value;     
 } else {
  //error
 }
}


I suppose the second code example you presented doesn't work...

I think it is because you're using the for in loop a little bit wrong.

for (var blah:String in somewhere){
    // blah represents a KEY of the somewhere object
    // to get the value of this key, use:
    var theValue = somewhere[blah];
}

It's the for each loop that loops through the values. But for now I'll use the for in.

Also, it's not in ObjectType, but rather in the class' prototype, that is in ObjectType.prototype.

So, to fix this:

for (var o:* in ObjectType.prototype) {
    if (value == ObjectType.prototype[o])
        this._type = value;     
    } else {
        //error
    }
}


You can solve this using reflection.

A similar question was asked just a few days ago, you should be able to use the same solution, found here.


It should be noted that while the the accepted answer is right, it's also really slow. Not something that you want to do a lot. There are three simpler solutions.

One: Check the value itself:

public function set type(value:String):void 
{
    if( value != ObjectType.ONE && value != ObjectType.TWO && value != ObjectType.THREE )
        return;
}

Obviously, the more constants you have the check the harder this becomes.

Two: Use ints as your constants

Change your ObjectType class to use ints:

public class ObjectType
{
    public static var NONE:int = 0;
    public static var ONE:int = 1;
    public static var TWO:int = 2;
    public static var THREE:int = 3;
    public static var TOTAL:int = 4;
}

Notice the NONE and TOTAL in there? This makes it easy to check if your value is in the right range:

public function set type(value:int):void 
{
    if( value <= ObjectType.NONE || value >= ObjectType.TOTAL )
        return;
}

You can add more values as needed and you just need to update TOTAL and it'll still work. This needs each value to be in order though.

Three: Use Enums

While Flash has no in-build class for enums, there's a lot of solutions available. Check our the Enum class from Scott Bilas: http://scottbilas.com/blog/ultimate-as3-fake-enums/

Using this as your base class your ObjectType class becomes:

public final class ObjectType extends Enum
{

    { initEnum( ObjectType ); } // static ctor

    public static const ONE:ObjectType = new ObjectType;
    public static const TWO:ObjectType = new ObjectType;
    public static const THREE:ObjectType = new ObjectType;

}

And your check now becomes:

public function set type(value:ObjectType):void 
{
    ...
}

Here, your setter now becomes type safe and will throw errors if anything other than an ObjectType is used.


It turns out that if using an ENUM type of check you should check for the constants property, not variables as showin in the example here: ActionScript - Determine If Value is Class Constant

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜