Simple Math Function Question Using Variables

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Knightlite
Posts: 51
Joined: Mon Jan 23, 2012 10:14 pm

Simple Math Function Question Using Variables

Post by Knightlite »

Hi,

I am have trouble trying to figure out how LiveCode handles simple math problems using variables.

I would like to do calculations involving the values contained in two text fields and put the result into a third text field.

In Basic you would do something like this:

Code: Select all

Dim I as integer
Dim C as integer
Dim X as integer

I = Val(txtI.Text)
C = Value(txtC.Text)

X= I * C

txtAnswer.Text = X
How would you do the same thing in LiveCode? The values of txtI and txtC are changing constantly, so they would not be a set number.

Cannot find anything about this in the documentation. Has to be something easy. I just can't find it.

Any help would be greatly appreciated.

Jim
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: Simple Math Function Question Using Variables

Post by dunbarx »

Whew. Ready?

put field "firstField" + field "secondField" into field "thirdField"

Not very readable, I know. Write back if you have problems. This short code segment assumes a single number in both argument fields.

You may love, or not, the fact that variables are not typed at all in LC. They are typed dynamically at compile time based purely on syntax. We love this.

Craig Newman
sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Simple Math Function Question Using Variables

Post by sturgis »

If you have fields named a, b, and c and want to sum a and b and put the result in c its pretty straight forward

put field "a" + field "b" into field "c"

You can also do the intermediate variable method
put field "a" into varA
put field "b" into varB
put varA + varB into field "C"

Of course you might need to do some validation..

if field "a" is a number.. or "is an integer"...


EDIT: Craig, you are just too too fast!
Knightlite
Posts: 51
Joined: Mon Jan 23, 2012 10:14 pm

Re: Simple Math Function Question Using Variables

Post by Knightlite »

Thanks so much Craig and sturgis. I really appreciate the help. I thought it was something like that. Thanks again for your really quick help.

Jim
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: Simple Math Function Question Using Variables

Post by dunbarx »

Sturgis:

This running joke among about a dozen of us, in the rush to be first, causes either errors to be made or silly approaches to be employed.

I do both.

Jacque seems to have put herself above all that nonsense. She is, after all, a lady.

Craig
sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Simple Math Function Question Using Variables

Post by sturgis »

So, what it boils down to is.. Not being a lady I have latitude to make my mistakes quickly in silly ways! (This actually isn't such a bad thing)

I am a king of silly approaches. Luckily some kind soul will eventually step in and yank me back from the brink.

Of course adding in large amounts of overdo and undersleep can lead to truly glorious messes.

I bow to the wisdom of lady logic, otherwise known as thinking before speaking. (or typing) One day i plan to try it.
Post Reply