Passing values to a method

Questions, tips and tricks and techniques for scripting in Servoy

Passing values to a method

Postby gstein » Sat Aug 21, 2004 5:00 pm

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!
gstein
 
Posts: 210
Joined: Tue Jul 27, 2004 2:38 pm

Postby patrick » Sat Aug 21, 2004 6:21 pm

no problem: you can call a method like this

Code: Select all
method(a, 1, b, 3, ...)


In the method "method" you do:

Code: Select all
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.
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Postby Morley » Sun Aug 22, 2004 1:02 am

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
Code: Select all
var a = 'text1';
var b = 'text2';
forms.company.MethodB(a, b);
In MethodB do this
Code: Select all
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.
Morley Chalmers
7Office Inc.
User avatar
Morley
 
Posts: 891
Joined: Fri Apr 25, 2003 4:54 pm
Location: Toronto, Canada

Postby gstein » Sun Aug 22, 2004 9:35 am

Thanks guys! That helps alot!!!
gstein
 
Posts: 210
Joined: Tue Jul 27, 2004 2:38 pm

Postby gstein » Sun Aug 22, 2004 10:44 am

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?
gstein
 
Posts: 210
Joined: Tue Jul 27, 2004 2:38 pm

Postby patrick » Sun Aug 22, 2004 11:29 am

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.
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Postby bcusick » Sun Aug 22, 2004 3:54 pm

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:

Code: Select all
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]
bcusick
 
Posts: 1255
Joined: Wed Apr 23, 2003 11:27 pm
Location: Thousand Oaks, CA USA

Postby Morley » Sun Aug 22, 2004 4:42 pm

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.
Morley Chalmers
7Office Inc.
User avatar
Morley
 
Posts: 891
Joined: Fri Apr 25, 2003 4:54 pm
Location: Toronto, Canada

Postby gstein » Mon Aug 23, 2004 8:06 am

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?
gstein
 
Posts: 210
Joined: Tue Jul 27, 2004 2:38 pm

Postby jcompagner » Mon Aug 23, 2004 10:56 am

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.
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8836
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet


Return to Methods

Who is online

Users browsing this forum: No registered users and 8 guests

cron