Forum Discussion

squirreltown's avatar
squirreltown
Roku Guru
11 years ago

array function prams

I have an array of aa's that are a part of a settings screen, 20 different buttons that do different things. I'm calling a function from each button. In the below Autopaint is a function being called.
butts = []
button={title:"RUN DEMO" , action:AutoPaint, sec:"demo", } : butts.Push(button)
butts[index].action(varr, m )
You can see, this way requires me to have the same function parameters for all the buttons. I can of course get around this in a clunky way using the button index, but I'm wondering if there is a slick way that allows me to put the function parameters in the array somehow?

Thanks

7 Replies

  • "squirreltown" wrote:
    I have an array of aa's that are a part of a settings screen, 20 different buttons that do different things. I'm calling a function from each button. In the below Autopaint is a function being called.
    butts = []
    button={title:"RUN DEMO" , action:AutoPaint, sec:"demo", } : butts.Push(button)
    butts[index].action(varr, m )
    You can see, this way requires me to have the same function parameters for all the buttons. I can of course get around this in a clunky way using the button index, but I'm wondering if there is a slick way that allows me to put the function parameters in the array somehow?

    Thanks


    What exactly are you trying to achieve? You could pre-cache all the arguments if they will always be the same for all button actions.
  • "Rek" wrote:
    What exactly are you trying to achieve? You could pre-cache all the arguments if they will always be the same for all button actions.

    It's no big deal, I already have it working fine by doing something like this:
    if index = 0
    butts[index].action(one set of params)
    else if index = 1
    butts[index].action(other set of params)
    end if

    I was just wondering if I could build the function params into the array somehow and just call
    butts[index].action(butts[index].params)
  •     butts = []
    button={title:"RUN DEMO" , action:AutoPaint, sec:"demo", params:{param1: arg1, param2: arg2}} : butts.Push(button)

    butts[index].action(butts[index].params)

    or,

    b = butts[index]: b.action(b.params)


    There are probably other, slicker, OOP-oriented ways to do that, depending on what the big picture of what you're trying to do is.
  • "belltown" wrote:
        butts = []
    button={title:"RUN DEMO" , action:AutoPaint, sec:"demo", params:{param1: arg1, param2: arg2}} : butts.Push(button)

    butts[index].action(butts[index].params)


    Rats. I tried that and it didn't work, it threw "wrong # of Func params" here:
    butts[index].action(butts[index].params)
    and not at the Line# of the function it's calling which does have 2 parameters.
    That aside, the function parameters (inside the parenthesis) should accept some sort of AA?
  • "squirreltown" wrote:
    "belltown" wrote:
        butts = []
    button={title:"RUN DEMO" , action:AutoPaint, sec:"demo", params:{param1: arg1, param2: arg2}} : butts.Push(button)

    butts[index].action(butts[index].params)


    Rats. I tried that and it didn't work, it threw "wrong # of Func params" here:
    butts[index].action(butts[index].params)
    and not at the Line# of the function it's calling which does have 2 parameters.
    That aside, the function parameters (inside the parenthesis) should accept some sort of AA?

    'action' only takes one param, an associative array. You'd need to extract the AA key values within 'action':

    function action (params as object) as void
    p1 = params.param1
    p2 = params.param2
    ' etc ...


    end function


    If you wrap the parameters in an AA, you can have as may as you want without changing the call to 'action'; just add them to the 'params' AA when you set up your buttons, and write whatever code you need in 'action' to handle them.
  • "belltown" wrote:

    If you wrap the parameters in an AA, you can have as may as you want without changing the call to 'action'; just add them to the 'params' AA when you set up your buttons, and write whatever code you need in 'action' to handle them.


    Ok! that worked, thank you. I'm finally getting to where it matters how i do a thing, not just the doing of the thing.
  • Since the params member is part of the calling AA, there's no need to pass any parameters at all:

    butts[index].action()

    And in the action function:

    function action() as void
    p1 = m.params.param1
    p2 = m.params.param2
    etc.

    However (you knew there was a however didn't you?), stylistically this isn't the greatest way to do things unless the parameters are fairly static. If you have to modify the params member of the AA just before making every call, it's better to explicitly pass them as parameters rather than doing something like this:

    butts[index].params.param1 = something
    butts[index].params.param2 = something_else
    butts[index].action()

    Hiding the fact that you're passing dynamic parameters to the function somewhat obfuscates the logic.

    --Mark