Optional parameter in method?

Questions, tips and tricks and techniques for scripting in Servoy

Optional parameter in method?

Postby vincent » Wed May 03, 2006 11:48 am

In servoy, can we create "Optional" parameter for method? Pls see below code for more details:

Function fDouble (Optional A)
If IsMissing(A) Then
fDouble = 1
Else
fDouble = A * 2
End If

:?:
vincent
 
Posts: 16
Joined: Thu Apr 20, 2006 10:57 am
Location: malaysia

Postby patrick » Wed May 03, 2006 11:52 am

Yes, you can call a method like this:

Code: Select all
fDouble(24)


and then in fDouble you do

Code: Select all
var A = arguments[0];  // receive the (first) argument
if (!A)
{
   ...
}


You can pass as many arguments as you like, since arguments are passed using the arguments array.

Hope this helps.
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Postby vincent » Wed May 03, 2006 12:06 pm

thanks patrick :D ,

I have try the method you've describe but .. what if there's more than 1 arguments,what will happen if i skip 1 arguments?

Normal method:
methodname(arguments[1],arguments[2],arguments[3])

Optional method:
methodname(arguments[1],arguments[3])

In Optional method,i've skip arguments[2]. I've try this in Servoy, and observe the following behaviour:

1.Arguments[3] become Arguments[2]
2.and Arguments[3] = undefined


Any help?
vincent
 
Posts: 16
Joined: Thu Apr 20, 2006 10:57 am
Location: malaysia

Postby patrick » Wed May 03, 2006 1:56 pm

If you pass arguments like

Code: Select all
method('a', 'b', 'c')


you fill the arguments array as

Code: Select all
arguments[0] = 'a'
arguments[1] = 'b'
arguments[2] = 'c'


So if you want to skip one argument, you have to do

Code: Select all
method('a', null, 'c')
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Postby bcusick » Wed May 03, 2006 1:56 pm

Hi Vincent,

Yes, you can do this:

Code: Select all
var arg1 = arguments[0]
var arg2 = ''

if(arguments[3] == undefined)
{
  var arg3 = arguments[1]
}
else
{
  var arg3 = arguments[2]
  arg2 = arguments[1]
}


Hope this helps.
Bob Cusick
bcusick
 
Posts: 1255
Joined: Wed Apr 23, 2003 11:27 pm
Location: Thousand Oaks, CA USA

Postby vincent » Thu May 04, 2006 2:56 am

Work like magic. :D This way i can create more generic method.

Thanks Bob and Patrick!
vincent
 
Posts: 16
Joined: Thu Apr 20, 2006 10:57 am
Location: malaysia

Re: Optional parameter in method?

Postby BulldogBen » Wed Jul 07, 2010 2:27 pm

Surely best to do it the way Patrick suggested?
Code: Select all
method('a', null, 'c')


That way you are being explicit about which arguments your are skipping.
27" iMac, MacOS 10.6.4
Servoy 5.2 / SQL Anywhere 11
Java 1.6.0_20
User avatar
BulldogBen
 
Posts: 58
Joined: Sun Jun 20, 2010 1:02 pm

Re: Optional parameter in method?

Postby ROCLASI » Wed Jul 07, 2010 2:59 pm

Hi Ben,

You do realize you are responding to a thread that is 4 years old and covers a pretty old version of Servoy ? :)

If you want to use optional values then you can use the following syntax:
Code: Select all
function myFunction(arg1, arg2, arg3) {
    arg3 = arg3||'My Default Value'; // if (false/empty/0/null/undefined) then use the default value

    // rest of your code...
}


Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Optional parameter in method?

Postby BulldogBen » Wed Jul 07, 2010 4:44 pm

Hint taken, I'll check the posting dates next time.

Damn forum newbies, you've got to love them!
27" iMac, MacOS 10.6.4
Servoy 5.2 / SQL Anywhere 11
Java 1.6.0_20
User avatar
BulldogBen
 
Posts: 58
Joined: Sun Jun 20, 2010 1:02 pm


Return to Methods

Who is online

Users browsing this forum: No registered users and 5 guests

cron