1 Reply Latest reply on Aug 4, 2006 12:30 PM by Newsgroup_User

    Build property names dynamically

    code1234 Level 1
      It there a way to build a property name dynamically, as a string or other data type, and convert and pass it as a property name?

      For example:

      I have a parent script, named 'data', with properties, as follows"

      property data1, data2, data3

      I have instanced the script in a global

      global gData
      set gData = new(script "data")

      data1 already has a value in it (1)

      I want to build the property names (data1, data2, etc) dynamically in variables using other behaviors, based on user input.

      When I try to build the name "data1" in the message window as a test like this"
      set myData = "data1"
      put gData.myData

      Instead of the value of property "data1" (which should be -- 1) I instead get "script error, property not found" #myData) This is not surprising as the contents of myData is a string.

      However, when I change it to a symbol
      set myData = symbol("data1")
      put gData.myData

      The message window returns the same thing.

      Also, 'put myData' returns -- #data1

      It seems the statement 'put gData.myData' is being interpreted like this: 'gData.#data1' instead of 'gData.data1' as it should

      I would normally just use globals for this type of thing, but this project requires as many behaviors (with doubleclick'able property setting) and parent scripts as possible to make it easier for others to modify in the future.

      Not sure if this is even possible, but any help would be appreciated.
        • 1. Re: Build property names dynamically
          Level 7
          Rather than using dot-syntax you can use bracket access and use strings
          for properties:
          global gData
          gData = new(script "data")
          myData = "data1"
          gData[myData] = "value associated with the property data1"
          put myData, gData[myData]

          Or convert your strings to symbols (assuming they're
          symbol-format-compliant):
          global gData
          gData = new(script "data")
          myData = "data1"
          myData = symbol(myData)
          gData[myData] = "value associated with the property data1"
          put myData, gData[myData]