Page 1 of 1
Putting the result into a field
Posted: Sun Jun 02, 2013 8:59 am
by ninjabunny14
Hi guys I have been working on a little project and I am now in the last stage of the proceedings but I have one small, probably very simple query
Here is my code (well part of it):
Code: Select all
put the currentChoice of button "Purchase Type" into tG4
put field "Price Input" into tG6
if btn "Purchase type" = "Private" then
if fld "Price Input" > 2000000.01 then
return "7%"
else if fld "Price Input" > 1000000.01 then
return "5%"
else if fld "Price Input" > 500000.01 then
return "4%"
else if fld "Price Input" > 250000.01 then
return "3%"
else
return "1%"
end if
else
return "15%"
end if
put (tG4, tG6) into field "UO %age"
In the end it returns nothing into field "UO %age"
What do I need to do?
Re: Putting the result into a field
Posted: Sun Jun 02, 2013 9:20 am
by snm
Change:
return "x%"
to:
put tG4 & " " & tG6 into field "UO %age"
in your if statement. Remove the last line.
Marek
Re: Putting the result into a field
Posted: Sun Jun 02, 2013 9:36 am
by ninjabunny14
Ok thank you will try this out
Will let you know if anything bad happens D:
Re: Putting the result into a field
Posted: Sun Jun 02, 2013 10:59 am
by ninjabunny14
Marek wrote:Change:
return "x%"
to:
put tG4 & " " & tG6 into field "UO %age"
in your if statement. Remove the last line.
Did that and suddenly the answer field started turning out massive numbers. All I am looking for is a way to find the returned result and put it in a field.
Thanks for the help
Re: Putting the result into a field
Posted: Sun Jun 02, 2013 11:50 am
by Klaus
Hi ninjabunny,
from the docs about "return":
...
Summary:
Stops the current handler and returns a value to the handler that called the current handle
...
Obviously you don't have a calling handler and so your values are RETURNed to "data nirvana"

That's why nothing ever happened in your intitial handler!
Best
Klaus
Re: Putting the result into a field
Posted: Sun Jun 02, 2013 12:55 pm
by SparkOut
See my answer and sample stack that I already provided here:
http://forums.runrev.com/phpBB2/viewtop ... 885#p76885
as also mentioned there,
Code: Select all
if btn "Purchase type" = "Private" then
will not help you because the button will never EQUAL "Private" but its label or menu history or custom property etc may. It's all explained in the original thread.
Re: Putting the result into a field
Posted: Mon Jun 03, 2013 6:15 pm
by ninjabunny14
Thank You SparkOut this is of much help. This now works out the percentage but when I then try to multiply this percentage by another figure nothing happens.
Code: Select all
function calculatePercentage pType, pPrice
-- the parameters passed to this function have been given more meaningful names
-- check if the pType parameter was "Private" or not
if pType = "Private" then
-- yes it is, so do our other checks on pPrice and return a value accordingly
if pPrice > 2000000.01 then
return "7%"
else if pPrice > 1000000.01 then
return "5%"
else if pPrice > 500000.01 then
return "4%"
else if pPrice > 250000.01 then
return "3%"
else
return "1%"
end if
else
-- no pType was not "Private" so return the appropriate value
return "15%"
end if
put (fld "Price Input" * fld "UO %age") into field "UO"
...
This is part of the function the rest is just sums depending on the values of field before it. Any Suggestions?
Re: Putting the result into a field
Posted: Mon Jun 03, 2013 6:33 pm
by Klaus
Take a look at my last posting and at your script and see if you can't find out WHY nothing gets summed up,
resp. why the last line of the script is never been executed!
How are you calling/using that function (if at all)?
Re: Putting the result into a field
Posted: Mon Jun 03, 2013 6:35 pm
by SparkOut
The function returns a
string literal containing the chars of the digit and % symbol to be stored in the field. LiveCode is not a strongly typed language and so it will do its best to make sense of what values you give to any operator. In this case, however, it won't be able to calculate a numeric value multiplied by a string. You may as well be asking it to give the answer "what do you get if you multiply 6 times idiotic?".
If you need to calculate with the value returned from the function then I would suggest you change the function to
or return 7, or return 5, etc, etc (without quotes, and without the % symbol) and store the numeric value in the field "UO %age". Add a label field to the screen showing the % symbol next to the "OU %age" field if you feel it looks better that way.
Then when you multiply the values, it will be numeric and calculate properly. 15 is not the same as 15% though, so multiplying by the returned value will be 100 times too much, of course, but you will be able to do
Code: Select all
put (fld "Price Input" * fld "UO %age" / 100) into field "UO"
BUT. Make sure the last calculation is done outside the function. You want a
handler to do some things, which involves calling a function (sending information to it, and expecting a returned value) and then do some things with that value - it's not part of the function.
Re: Putting the result into a field
Posted: Mon Jun 03, 2013 6:47 pm
by SparkOut
Try this new sample stack (unzip first)
Re: Putting the result into a field
Posted: Tue Jun 04, 2013 6:52 pm
by ninjabunny14
Ok thanks will let you know if it all works (HOPEFULLY I DON'T MESS UP AGAIN

)
Re: Putting the result into a field
Posted: Tue Jun 04, 2013 7:05 pm
by ninjabunny14
ninjabunny14 wrote:Ok thanks will let you know if it all works (HOPEFULLY I DON'T MESS UP AGAIN

)
Ok that didn't work

Here is the whole thing:
Card "Stamp Duty Calculator" Script:
Code: Select all
function calculatePercentage pType, pPrice
-- the parameters passed to this function have been given more meaningful names
-- check if the pType parameter was "Private" or not
if pType = "Private" then
-- yes it is, so do our other checks on pPrice and return a value accordingly
if pPrice > 2000000.01 then
return (7/100)
else if pPrice > 1000000.01 then
return (5/100)
else if pPrice > 500000.01 then
return (4/100)
else if pPrice > 250000.01 then
return (3/100)
else
return (1/100)
end if
else
-- no pType was not "Private" so return the appropriate value
return 15
end if
end calculatePercentage
on Calculate
put (fld "UO" * (45/100)) into field "ITS fee"
put (fld "ITS Fee" * (20/100)) into field "VAT"
put (fld "ITS Fee" + fld "VAT") into fld "Total Fee"
put (fld "UO" - fld "Total Fee") into fld "Savings"
end Calculate
Button code:
Code: Select all
on mouseUp
-- get the values from the field and the menu button label and put into variables
-- simply to make it easier to pass them to the function
put the label of button "Purchase Type" into tPurchaseType
put field "Price Input" into tPrice
-- pass those values to the function (which is on the card script so all the buttons on the card can use it)
-- and take the returned value from the function
-- and store it in the destination field
put calculatePercentage (tPurchaseType, tPrice) into field "UO %age"
-- this sends the values in variables tPurchaseType and tPrice to the calculatePercentage function
-- gets the value returned from the function and puts it in the field
put (fld "Price Input" * fld "UO %age") into field "UO"
send "Calculate" to card "Stamp Duty Calculator"
end mouseUp
I can't believe I still have something wrong I am so incompetent. I tried 3 things with with this:
1) I added the calculate code to the button instead
2) I added it to the card (as is above)
3) I changes the outcomes of the if statement so that the code would follow BODMAS (as is above)
All 3 got different results. Where have I gone wrong THIS time?
Apologies and many thanks
ninjabunny14
