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
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
Yes, you can call a method like this:
fDouble(24)
and then in fDouble you do
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.
thanks patrick ,
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?
If you pass arguments like
method('a', 'b', 'c')
you fill the arguments array as
arguments[0] = 'a'
arguments[1] = 'b'
arguments[2] = 'c'
So if you want to skip one argument, you have to do
method('a', null, 'c')
Hi Vincent,
Yes, you can do this:
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.
Work like magic. This way i can create more generic method.
Thanks Bob and Patrick!
Surely best to do it the way Patrick suggested? ```
method(‘a’, null, ‘c’)
That way you are being explicit about which arguments your are skipping.
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:
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.
Hint taken, I’ll check the posting dates next time.
Damn forum newbies, you’ve got to love them!