calculations...again

i have some code that calculates a value based on a parameter;

i envision having several calculated fields on a form, and
each of them would call this common calculation, passing
a paramater specifiec to each of them, and each field would display
the results of the calculation on the form.

however, i can’t pass an argument to a calculation, so that idea
is out.

i seem to remember reading on the forum that i’ts NOT ok for
a calculated field to call a global method (correct me if i’m wrong),
so the idea of a global method containing the formula is out as well.

it seems that the only option i have, is to have all 4 calculated fields
on the form each have their own copy of the formula, which breaks
all the rules of good programming practice.

can anyone tell me of a better way to do this in servoy?

Can’t you do something like this:

  1. change your calculated fields into plain integer/number(?) fields.
  2. create global script with the formula
  3. create onShow script in the form that looks like:
    myDatabaseColumn1 = globals.myFormula(myParameterx);
    myDatabaseColumn2 = globals.myFormula(myParametery);
    etc…

note: of course your calculation fields will now only be filled/refreshed after actually opening the form. (I can’t oversee from here if this would cause inconsistency in your solution)

if you have a common script that you want to call in calculations. Just make a global method and call that one. (globals.methodName(arguments))

you can’t see that in the tree but it will work fine as long as you don’t do there things like accessing forms and/or elements ect.

maarten:
note: of course your calculation fields will now only be filled/refreshed after actually opening the form. (I can’t oversee from here if this would cause inconsistency in your solution)

that’s basically the approach i’m considering, but as you noted,
my fields won’t automatically update (which is the real beauty
of calculated fields).

so it looks like i’ll have to emulate the calculation concept, and
have a method that gets called when any of the fields containing
the input parameters changes.

thanks for helping me think this thru.

rm.

so it looks like i’ll have to emulate the calculation concept, and
have a method that gets called when any of the fields containing
the input parameters changes.

well, in fact if you follow johan’s suggestion, you can keep your calculation and fill it with:

var exampleArgument = x + y //or what ever
return globals.myFormula(exampleArgument , myDatabaseColumn, etc...);

jcompagner:
if you have a common script that you want to call in calculations. Just make a global method and call that one. (globals.methodName(arguments))

you can’t see that in the tree but it will work fine as long as you don’t do there things like accessing forms and/or elements ect.

actually, that would be great. i assumed since i couldn’t see global methods
in the tree that it was not a good idea to call them. that helps a great deal.

are there any other restrictions when calling global methods from a calculation?
like, can i use relations to get data from other tables?
can i do sql queries?

thanks.
rm

out of my head:

Everything can be done in such a global method, Except everything under the “forms” node.

jcompagner:
Everything can be done in such a global method, Except everything under the “forms” node.

got it. thanks.