Quick Total Form

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
BeatleBailey
Posts: 4
Joined: Wed Jan 26, 2011 3:07 pm

Quick Total Form

Post by BeatleBailey » Sat Jan 29, 2011 7:36 pm

Hello all,

This is my first shot at livecode. I cant seem to get anything in this code to work
other then one beep. The doTotal function is not found and does not run, I assume.
I have pasted in the scripts in use as well as attached the livecode stack. Any help
would be greatly appreciated.

BB

// This script is in a button called optTotal

on mouseup
beep
call doTotal
end mouseup

// this script is in the main stack called qwkForm

global gVarDesc
global gVarSubTotal
global gVarTotal
global gVarShipping

function doTotal

if optPC = "New" then
put "Big New PC with " into gVarDesc
gVarSubTotal = 100
gVarShipping = 50
else
put "Small Used PC with " into gVarDesc
gVarSubTotal = 50
gVarShipping = 35
end if

if optHD = "Big" then
put gVarDesc + "Big Hard Drive" into
gVarSubtotal = gVarSubTotal + 75
gVarShipping = gVarShipping + 10
else
put gVarDesc + "Small Hard Drive" into
gVarSubtotal = gVarSubTotal + 45
gVarShipping = gVarShipping + 5
end if

put gVarDesc into fldDesc
put gVarSubtotal into fldSubTotal
put gVarShipping into fldShipping
put gVarSubtotal + gVarShipping into fldTotal

end doTotal
Attachments
sampleForm.zip
(1.19 KiB) Downloaded 273 times

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

Re: Quick Total Form

Post by dunbarx » Sat Jan 29, 2011 8:15 pm

You obviously have other programming experience. There are many syntactic problems with your script. I did not even look at the methodology, which might be perfectly sound, though a bit wordy.

The main issues are:

1- LiveCode does not support a statement like gVarSubTotal = 100.

Instead, use the form "put 100 into gVarSubTotal". See the dictionary under "put".

2- In the statement: put gVarDesc + "Small Hard Drive" into

you have no target container to receive the sum (put into what?) . Also, you cannot add a piece of text to a number, as in: add 5 to "Small Hard Drive".

3- You are calling a function, but really are calling handler. You need to learn more about the difference between these two, and how they are used. In fact, you really only need to write:

on mouseup
beep
doTotal
end mouseup

If you step through the debugger, where about a dozen or more errors will pop up, you can repair your script line by line. Watch the values of the variables, and note that literal strings cannot be used the way you are doing it. Lose those globals; they are superfluous.

I corrected all your errors below. I did so by simply fixing the syntax, not by fixing your script. The results will be garbage, but it does at least run. Compare the actions of each line with the errors in the original. You should quickly see how to make yours work correctly, that is, where you need to tidy up the original. You do know how to debug, right?

The good news is that you will have come a long way toward overcoming that first hump in getting LiveCode into your system. Welcome aboard. You are in for a treat. And post back endlessly if you need to. I am only being so helpful because I am the first to reply. There are dozens out there who will be just as eager...

Code: Select all

on mouseup
beep
doTotal
end mouseup

// this script is in the main stack called qwkForm

global gVarDesc
global gVarSubTotal
global gVarTotal
global gVarShipping

on doTotal
   
   if optPC = "New" then
      put "Big New PC with " into gVarDesc
      put 100 into gVarSubTotal-- NOT gVarSubTotal = 100
      put 50 into gVarShipping
   else
      put "Small Used PC with " into gVarDesc
      put 50 into gVarSubTotal
      put 35 into gVarShipping 
   end if
   
   if optHD = "Big" then
      put gVarDesc + 10 into temp
      put gVarSubTotal + 75 intogVarSubtotal
      put gVarShipping + 10 into gVarShipping
   else
      put 2 + 5 into temp2
      put 3 + 45 into gVarSubtotal
      put 4 + 5 into gVarShipping
   end if
   
   put gVarDesc into fldDesc
   put gVarSubtotal into fldSubTotal
   put gVarShipping into fldShipping 
   put gVarSubtotal + gVarShipping into fldTotal
   
end doTotal

Craig Newman

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Quick Total Form

Post by bn » Sat Jan 29, 2011 9:31 pm

Hi BeatleBailey,

Craig gave you good advice, I think it is very important to go through the sample stacks and the lessons. You just cant make the language up, it has some rules. But once you get the basics you will quickly be able to do stuff like this.

I did a version of your stack that does what you want and I append it below.

Kind regards

Bernd
Attachments
sampleForm.livecode.zip
(1.64 KiB) Downloaded 260 times

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

Re: Quick Total Form

Post by dunbarx » Sat Jan 29, 2011 9:43 pm

See? I told you.

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Quick Total Form

Post by bn » Sat Jan 29, 2011 9:48 pm

Hi Craig,

You were too fast for me :)

Kind regards

Bernd

BeatleBailey
Posts: 4
Joined: Wed Jan 26, 2011 3:07 pm

Re: Quick Total Form

Post by BeatleBailey » Sun Jan 30, 2011 4:21 am

I thank you both for your responce. I am not a programmer by trade, just a hobbyist . I have used several languages in the past and settled on Visual Basic as my favorite. Not only was it the easiest to understand when writing but you can go back to the code years later and still easily understand what it does without any descriptive commenting in the code.

Anyway Im off to test out your fixes and advice. Thanks one again

BB

BeatleBailey
Posts: 4
Joined: Wed Jan 26, 2011 3:07 pm

Re: Quick Total Form

Post by BeatleBailey » Sun Jan 30, 2011 4:52 am

Hello Again

With the examples and comments posted here I was able to update the code to do as I desired.
Just thought I would pop back in to paste in the code for my example to complete this thread.
Must keep things nice and tidy! I hate it when people post their questions in a forum, get a few replies, work out their problems and dont come back to share for the next person. I have also attached the livecode sample as well. I will be back again the next time I'm displaying a puzzled look on my face. I'm sure it wont be long!

Thanks Again
BB

-- this script is in the optMenus
on mouseup
doTotal
end mouseup

-- This script is in the card

global gVarDesc
global gVarSubTotal
global gVarTotal
global gVarShipping

on doTotal

if the label of btn "optPC" of card 1 = "New" then
put "Big New PC with " into gVarDesc
put 100 into gVarSubTotal
put 50 into gVarShipping
else
put "Small Used PC with " into gVarDesc
put 50 into gVarSubTotal
put 35 into gVarShipping
end if

if the label of btn "optHD" of card 1 = "Big" then
put gVarDesc & "Big Hard Drive" into gVarDesc
put gVarSubtotal + 75 into gVarSubtotal
put gVarShipping + 10 into gVarShipping
else
put gVarDesc & "Small Hard Drive" into gVarDesc
put gVarSubtotal + 45 into gVarSubtotal
put gVarShipping + 5 into gVarShipping
end if

put gVarDesc into field "fldDesc"
put gVarSubtotal into field "fldSubTotal"
put gVarShipping into field "fldShipping"
put gVarSubtotal + gVarShipping into field "fldTotal"

end doTotal
Attachments
sampleForm1.zip
(1.32 KiB) Downloaded 249 times

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Quick Total Form

Post by bn » Sun Jan 30, 2011 10:27 am

Hi BeatiBiley,

nice, you added the description field and it works.
Just a short comment of the way you use global variables. They are not needed here.
In Livecode you have local variables, these exist within a handler (that is what I call a function or a command or a script that reacts to a message like on mouseUp) and are accessible only to the code inside this handler. They are gone once the handler finishes. You dont have to declare them, i.e. you can start using a local variable by just using it.
put 50 into tMyVariable
creates that variable and puts 50 into it. It does not hurt to declare a

Then there are script local variables, you have to declare them outside of any handler, usually at the top of a script (any script, like object script, card or stack script). These are accessible to any handler inside this script, e.g. the card script. They will persist as long as the program runs and you dont have to declare them inside a handler of that script.

local sMyVariable
on myCommand
put 10 into sMyVarible
end myCommand


Then there are global variables. These have to be declared outside of a handler or inside of a handler with the word global. Once you declare a variable as global it can be accessed by any handler in any stack that is open. There you would have to declare the variable again and you can use it.

global gMyVariable

on myCommand
put 3 into gMyVariable
end myCommand

if you declare that variable outside of any handler any handler of that script has access to the global variable without having to declare it again inside of the handler.

Please have a look at the User Guide page 140 + the section on variables.

Now you declare a global variable that is not used except inside the one handler in the card script. Here you dont need a global variable, a local variable would be sufficient.

All this to say you would want to just declare the type of variable that you need. If you would do all your working in global varibles then it can be really confusing to remember what handler does what to the variable.
And since it is available to all open stacks and substacks that could many different handlers that use that variable.

Prepending varibles with a letter indicating their scope is a good idea, just like you did with the global variables you used. I use t for script variable, s for script local variables and g for global variables. Many of the people here do it, but you are free to have your own convention. It makes reading a script later easier.

And do by all means have a look at the the resource center and the start center. You can access them under the Help menu.
And look at
http://lessons.runrev.com/
There are lots of specific examples.

And there is the scripting conferences
http://www.runrev.com/developers/lesson ... nferences/
also a very nice introduction to livecode scripting.

Kind regards

Bernd

BeatleBailey
Posts: 4
Joined: Wed Jan 26, 2011 3:07 pm

Re: Quick Total Form

Post by BeatleBailey » Sun Jan 30, 2011 5:18 pm

Hello again,

I knew it wouldnt be long before I was back. I switched my variables to local and renamed them to what I am used to. Now I have added another option to my "quick total form" called optWarranty. This has three menu items at run time. Lets say if someone selected "New PC" I would like the optWarranty to display "2 Year" and "1 Year" as options. If someone selects "Used PC" I would like the optWarranty to display "1 Year" and "30 Day". How do I load these options into the optWarranty when the user makes selections in "optPC".

I have tryed enable/disable menuItem with no luck and adding and removing menuItems with no luck either. My
script was such a mess i cleaned it up again and hope someone can guide me in the right direction. Here is my current script. I have also attached my livecode sample.

local strTemp
local strDesc
local intSubTotal
local intTotal
local intShipping

on doTotal

if the label of btn "optPC" of card 1 = "New" then
put "Big New PC and " into strDesc
put 100 into intSubTotal
put 50 into intShipping
else
put "Small Used PC and " into strDesc
put 50 into intSubTotal
put 35 into intShipping
end if

if the label of btn "optHD" of card 1 = "Big" then
put strDesc & "Big Hard Drive with " into strDesc
put intSubtotal + 75 into intSubtotal
put intShipping + 10 into intShipping
else
put strDesc & "Small Hard Drive with " into strDesc
put intSubtotal + 45 into intSubtotal
put intShipping + 5 into intShipping
end if

put label of btn "optWarranty" of card 1into strTemp
switch strTemp
case "2 Year"
put strDesc & "Two Year Warranty" into strDesc
put intSubtotal + 50 into intSubtotal
break
case "1 Year"
put strDesc & "One Year Warranty" into strDesc
put intSubtotal + 25 into intSubtotal
break
case "30 Day"
put strDesc & "30 Day Warranty" into strDesc
end switch

put strDesc into field "fldDesc"
put intSubtotal into field "fldSubTotal"
put intShipping into field "fldShipping"
put intSubtotal + intShipping into field "fldTotal"

end doTotal
Attachments
sampleForm2.zip
(1.51 KiB) Downloaded 264 times

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Quick Total Form

Post by bn » Sun Jan 30, 2011 5:46 pm

Hi BeatleBailey,

replace the script of button "optPC" with

Code: Select all

on menupick theItem
   switch theItem
      case "used"
         set the text of button "optWarranty" to  "1 Year" & cr & "30 Day" 
         set the label of button "optWarranty" to "1 Year"
         break
      case "New"
         set the text of button "optWarranty" to  "2 Year" & cr & "1 Year" 
         set the label of button "optWarranty" to "2 Year"
         break
   end switch
   doTotal
end menupick
that should do it.

No more mouseUp handler here, although you could just as well do this in a mouseUp handler.



You still initialize your variables as script local variables. Not necessary. You could just as well delete the initialization outside of the handler in the card script. Try to comment them out and see that it still does work.

And then you guess why. :)

Kind regards

Bernd

Post Reply