开发者

Can no longer assign class property values using this["propertyName"] in Flex

开发者 https://www.devze.com 2023-01-22 15:48 出处:网络
Given a dynamic or non-dynamic class like the following: package { public class MyClass { public var myProperty:String;

Given a dynamic or non-dynamic class like the following:

package {
  public class MyClass {
    public var myProperty:String;
    public var myBooleanProperty:Boolean;

    public function MyClass() {}
  }
}

Flex 3 allows you to assign a value to myProperty like this:

myClassInstance["myProperty"] = "myValue";
myClassInstance["myBooleanProperty"] = true;

I regularly parse XML to开发者_Go百科 get property names and their values then update correlated classes using this technique; however, Flex 4 no longer allows assigning the boolean property. I don't have a work-around.

If you trace the results:

trace(myClassInstance.myProperty) // Returns "myValue"
trace(myClassInstance.myBooleanProperty) // Returns null

Can someone explain what has changed and how to work-around the issue?


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               creationComplete="application1_creationCompleteHandler(event)"
               >
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                var c:MyClass = new MyClass();
                c["myBooleanProperty"] = true;
                trace(c["myBooleanProperty"]);
            }

        ]]>
    </fx:Script>
</s:Application>

This outputs "true" with the Flex SDK 4.1. There may be something else wrong in your code?


You can use Dictionary class instead.


Have you simply tried to do this?

  myClassInstance.myBooleanProperty = true;

This is actually the standard way of assigning a value to a public property in AS3.

0

精彩评论

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