Passing values to a method

Is it possible to pass a value to a method?

I see in the editor that all methods have () at the end – which makes it seem like it would be possible to pass a value to a method – but if so its not clear to me how.

Thanks for your help!

no problem: you can call a method like this

method(a, 1, b, 3, ...)

In the method “method” you do:

var param1 = arguments[0];
var param2 = arguments[1];
var param3 = arguments[2]; etc.

In short: when calling a method you can pass an array called “arguments” to that method.

Just starting out this one stumped me as well. The solution is actually quite simple, but the docs left me scratching my head, too sparce for my slow uptake.

In MethodA do this

var a = 'text1';
var b = 'text2';
forms.company.MethodB(a, b);

In MethodB do this

var a = arguments [0];
var b = arguments [1];

This has to do with arrays which I don’t yet clearly understand. As best as I can make out in MethodA you’re calling on MethodB to act and simultaneously sending an array of the two variables by putting them within the parentheses. In MethodB you’re picking them up as “arguments”, which start from 0.

I understand you can pass as many variables as you like this way. Perhaps someone else can fill in some of the theoretical gaps. But the above works nicely.

Thanks guys! That helps alot!!!

Actually I spoke too soon. I still have a problem.

I see how to pass a value to a method, but can I do that from an event on a form – rather than from inside another method.

For example, on the onShow event of myForm I would like to do something like myMethod(1,“hello”), but I can’t seem to type in the onShow box – all I can do is select an existing method. So even if I setup myMethod to accept parameters, it doesn’t seem like I can call it from the form directly.

Is there a way around this?

no. you need to attach a method to the property that passes the desired parameters to some other method. So you need one dedicated method for that.

You can also use: application.getMethodTriggerElementName() and/or application.getMethodTriggerFormName() to detect the named element or form name.

Using application.getMethodTriggerFormName() you can have a single global “onShow” method that looks like this:

var frm = application.getMethodTriggerFormName()

if(frm == 'Form1')
{
     //do form1 stuff here
}
elseif (frm == 'Form2')
{
     //do form2 stuff here
}
else
{
     //do other form stuff here
}

Hope this helps,

Bob Cusick[/code]

Interesting. Had not noticed those functions.

Would be useful seed the docs with examples such as the above. With many of the functions it’s hard at times to imagine what situations they are designed to solve. I know, I know, the core code of Servoy is evolving very quickly necessitating major rewrites of the docs. All in good time.

In the meantime regular reading and participation in this forum is clearly the best way to ginger one’s imagination.

Bob,

This is way cool, but what I really need is the name of the event which called the method. Is there a way to get that?

no you can’t
If you want to be completely dynamic then you could make a few global methods like:

globalOnShow()
globaOnLoad()

and then you attache those to youre methods. Based on that you call the real thing from the current form.