Forum Discussion

jbrave's avatar
jbrave
Channel Surfer
15 years ago

objects and functions question

Hi,

I have a function like this:

bigfunc=init()

function init() as object
return {label:functionname,lable1:function1name,lable2:function2name}
end function


(really, a huge pile of functions in one object)

I have an array of items which each have an action with the same name as the lable in bigfunc:

item[1].action="lable1"
item[1].id="23423452"
item[2].action="lable2"
item[2].id="23256562"


what I want to do is to use the value stored in item.action to execute the function when the item is selected, but do it using the lable in the BigFunc array:

this=item[1].action
id=item[1].id

bigfunc.this(id)

it's not working, I'm getting:

Member function not found in BrightScript Component or interface

I tried simplifying and bypassing bigfunc to directly call the function:

this="function1name"
this(id)

but I get:

Member function not found in BrightScript Component or interface.

I can explicitly call bigfunc.lable1(id)

and the function executes.

is there a way to do this without using exec()

- Joel

2 Replies

  • "jbrave" wrote:

    this="function1name"
    this(id)

    but I get:

    Member function not found in BrightScript Component or interface.

    I can explicitly call bigfunc.lable1(id)

    and the function executes.

    is there a way to do this without using exec()

    - Joel


    That's PHP style, which is sort of it's own unique PHP thing (go figure).

    I'm not sure, but if it's a regular function, you may have to use eval(). If it's actually a method on an object, you are in luck. Gander at this:

    function func1()
    print m.v1
    end function

    manualObj = {
    v1: 1
    f1: func1
    f2: func2
    }

    print manualObj.v1 ' prints "1"
    print manualObj["v1"] ' also prints "1"
    varname = "v1"
    print manualObj[varname] ' prints "1" yet again

    manualObj.f1() ' prints "1"
    manualObj["f1"]() ' also prints "1"
    funcname = "f1"
    manualObj[funcname]() ' prints "1" yet again


    The thing to remember is that a variable can contain a function, just as it can contain a value. In this case, a function is just a special type, like an int or string. In a language this is called "first class functions" (http://en.wikipedia.org/wiki/First-class_function). It's different than PHP, much more powerful and actually more common. The way[ to determine whether you are calling the function or accessing it's code/contents is whether you append the parenthesis (manualObj.f1 vs manualObj.f1() above).
  • jbrave's avatar
    jbrave
    Channel Surfer


    funcname = "f1"
    manualObj[funcname]() ' prints "1" yet again




    This looks like what I'm looking for,

    Thanks!

    -Joel