Testing if function exists...

Hi All,

I use the following method to test for whether the method is exsit
the it execute the mehtod else skip, it worked fine for me with
Servoy 3.5.5/6.

if (forms.testMethod != uindefined) {

forms.testMethod()
}
else {
do something
}

Thanks
Kelvin

SV Kelvin,

Thanks.

Ok…well:

  1. try catch works, but make sure to throw(err) in the catch part so that Servoy can debug and log errors.

  2. != undefined works - thanks SV.

  3. And now… the original if (form.method) works too ! ! Why it wasn’t before is beyond me…arghh.

So, ignore everything…although the discussion did draw out a few ways to check if methods exist :slight_smile:

Agile: Yes, I had checks in the script to throw an error and return if they weren’t set as I was troubleshooting. Thanks for taking the time…wish i knew what it was doing earlier that it wasn’t working for me.

NCM
FSCI

In general, however, I would recommend you actually test whether it’s a “function” type. Especially with the addition of form variables in Servoy 4, unless you are careful about naming conventions, it’s more and more likely you could have some other non-function declared on the form with the same name.

greg.

Yes, I use a strict naming system:

fm_ for form methods
mv_ for local method variables
gv_ global variables
r_ relationships
gr_ global relationships
btn_ button elements
fld_ field elements
c_ calc fields
sum_, count_ etc. for aggregate fields

but, yes, checking if its a function type would add another layer and be useful in case there was overlap.