Page 1 of 1
Simple Math Function Question Using Variables
Posted: Mon Mar 26, 2012 8:55 pm
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
Re: Simple Math Function Question Using Variables
Posted: Mon Mar 26, 2012 9:07 pm
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
Re: Simple Math Function Question Using Variables
Posted: Mon Mar 26, 2012 9:10 pm
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!
Re: Simple Math Function Question Using Variables
Posted: Mon Mar 26, 2012 9:20 pm
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
Re: Simple Math Function Question Using Variables
Posted: Mon Mar 26, 2012 10:45 pm
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
Re: Simple Math Function Question Using Variables
Posted: Mon Mar 26, 2012 10:56 pm
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.