Page 1 of 1

Functions?

Posted: Sat Jun 18, 2011 9:43 am
by Jason1234
Hi.. This is my first post. I am new to Livecode!...

I am sorry if this question is obvious but I have searched the help file and forum and cannot find an answer that relates to my question. Please can someone help me?

I have three fields: Field1 Field2 Result I am using the following code that is executed when the user leaves each field

Code: Select all

on closefield
      if the text of fld "Field1" is a number or the text of fld "Field2" is a number then
         put fld "Field1" + fld "Field2" into fld "Result"
         End if
end closefield
My full application will have a lot more going on but I need to work out the principle first.... I need to "call" this code from each field so that when the code is changed I don't have to go to every location and edit it. I understand that if you don't enter script into the field the message is passed up to the stack... this is OK.. but what if I want to have different maths calculations stored and then executed from different fields. Again the maths will be involved so I do not want to have to edit in each object only in one place.

I hope you can understand what I am trying to do... my finished application will have the requirement to perform a number of maths calculations in script and I only want to edit this script if it has an error in one place.

Any help appreciated....

Thanks

Re: Functions?

Posted: Sat Jun 18, 2011 10:55 am
by Klaus
Hi Jason,

of course you could:
...
send "closefield" to fld "your field here"
...

But the better way is to create an extra handler for the calculation
and use this "on closefield". (re-usable code)

This way you can use the handler wherever you need to!

Put this into the stack (or card) script:

Code: Select all

command do_the_calculations
   if the text of fld "Field1" is a number or the text of fld "Field2" is a number then
      put fld "Field1" + fld "Field2" into fld "Result"
   End if
end do_the_calculations

## then use it like this in every field you like:
on closefield
    do_the_calculations
end closefield
You get the picture :)

You might need to add a descriptor to use in the stack:
...
if fld "Field1" OF CD 2 ...
...


Best

Klaus

Re: Functions?

Posted: Sat Jun 18, 2011 11:12 am
by Jason1234
Just perfect...! Thank you for your help.

Re: Functions?

Posted: Sat Jun 18, 2011 11:45 am
by bn
Hi Jason,

in the same sense as what Klaus said:

I made a little stack that has 1 card and 2 groups with 3 fields each. Every field jsut has

Code: Select all

on closefield
   myTestRoutine
end closefield
in the card script is the code to handler myTestRoutine.

The first group of fields adds numbers and the second group of field assembles a string. What the fields do depends on the name of the group.
This would be a way to determine what the fields are supposed to do by just grouping them and giving the group a name.
It sounds a little complicated but if I understand your project this is what you want to do: different groups of fields have different behavior.

The stack uses the target, the owner, the long ID, and the short name to distiguish the calling fields and the group they belong to.
To understand these things might be helpful in your project. It makes coding a lot more efficient.
There are more ways to tackle this sort of things, but this is one of them.
callingHierarchy.livecode.zip
(1.64 KiB) Downloaded 260 times

Oh, and welcome to the forum

Kind regards

Bernd

Re: Functions?

Posted: Sat Jun 18, 2011 12:03 pm
by Jason1234
Hi & thank you for the welcome....

I appreciate the file too this will help me...

I am so impressed with the help received via the Forum ... Thank you!