Functions?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Jason1234
Posts: 42
Joined: Sat Jun 18, 2011 9:20 am

Functions?

Post by Jason1234 » Sat Jun 18, 2011 9:43 am

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
Windows / MAC / IOS / Android - Deployment
Build 5.5.4 / & Community Version 6.1

Klaus
Posts: 13872
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Functions?

Post by Klaus » Sat Jun 18, 2011 10:55 am

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

Jason1234
Posts: 42
Joined: Sat Jun 18, 2011 9:20 am

Re: Functions?

Post by Jason1234 » Sat Jun 18, 2011 11:12 am

Just perfect...! Thank you for your help.
Windows / MAC / IOS / Android - Deployment
Build 5.5.4 / & Community Version 6.1

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4028
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Functions?

Post by bn » Sat Jun 18, 2011 11:45 am

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 201 times

Oh, and welcome to the forum

Kind regards

Bernd

Jason1234
Posts: 42
Joined: Sat Jun 18, 2011 9:20 am

Re: Functions?

Post by Jason1234 » Sat Jun 18, 2011 12:03 pm

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!
Windows / MAC / IOS / Android - Deployment
Build 5.5.4 / & Community Version 6.1

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”