1 Reply Latest reply on Nov 16, 2007 7:28 AM by Dewin_Cymraeg

    Updating properties by name

    Dewin_Cymraeg
      I have a need to update the property of a class given its name.

      I have found that if I used the notation :

      obj["name"] = "hello world!"

      that this does not update the property 'name' of sealed class instance 'obj'.

      To be more explicit, suppose I have the following class definition:

      public class Foo
      {
      private var _name : String = "";

      public function get name() : String
      {
      return _name;
      }

      public function set name(name : String) : void
      {
      if (name == null) // protect from setting it as null, for some reason...
      name = "";
      _name = name;
      }
      }

      and then I have some code that knows that an object it has a reference to has a property called 'name', but that this is generic code and the object reference it has is not explicit. So, I was hoping to be able to do this:

      public function setProperty(instance : Object, propertyName : String, propertyValue : Object) : void
      {
      instance[propertyName] = propertyValue;
      }

      However, this doesn't work, as it tries to create a new dynamic property and can't.

      Is there another way to set the property value, similar to the way you can set a property on a JavaBean?

      Thanks!

      Rhys

      PS Apologies for the code not being formatted properly. I can't find the 'attach code' feature that I keep reading about.