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!
Hi...noob question here, but I have tried this multiple ways and have no answer on how to structure my if/else statement without it verifying one of the 4 conditions and moving to the "else" statement. Here is my script and I'll explain...
if vTS is not 0
then answer "Trauma Supplies missing from inventory. Please go back and complete Trauma section"
if vWhite is not 0
then answer "ALS supplies missing from inventory. Please go back and complete ALS section"
if vMisc is not 0
then answer "Miscellaneous Supplies missing from inventory. Please go back and complete Miscellaneous section"
if vAirway is not 0
then answer "Airway supplies missing from inventory. Please go back and complete Airway section"
else
put "Currently Stocked Items" & cr & fld "iot" card "Send" & cr & cr & "Items Needing Re-stocked" & cr & fld "not" card "Send" into vBody
mobileComposeMail "Test E-mail", someEmail@verizon.net,,, vBody, tAttachment
end if
end mouseUp
I want to check that all 4 conditions are true before triggering the "ELSE" statement, but what seems to happen is that as long as one condition is true, it goes to the "ELSE" statement instead of stopping. I have tried to make this into a "Switch" but I have the same issue....any ideas on what I am doing wrong??
on mouseUp
if vTS is not 0
then
answer "Trauma Supplies missing from inventory. Please go back and complete Trauma section"
exit mouseUp
end if
if vWhite is not 0
then
answer "ALS supplies missing from inventory. Please go back and complete ALS section"
exit mouseUp
end if
if vMisc is not 0
then
answer "Miscellaneous Supplies missing from inventory. Please go back and complete Miscellaneous section"
exit mouseUp
end if
if vAirway is not 0
then
answer "Airway supplies missing from inventory. Please go back and complete Airway section"
exit mouseUp
end if
put "Currently Stocked Items" & cr & fld "iot" card "Send" & cr & cr & "Items Needing Re-stocked" & cr & fld "not" card "Send" into vBody
mobileComposeMail "Test E-mail", someEmail@verizon.net,,, vBody, tAttachment
end mouseUp
on mouseUp
if vTS is not 0
then
answer "Trauma Supplies missing from inventory. Please go back and complete Trauma section"
else
if vWhite is not 0
then
answer "ALS supplies missing from inventory. Please go back and complete ALS section"
else
if vMisc is not 0
then
answer "Miscellaneous Supplies missing from inventory. Please go back and complete Miscellaneous section"
else
if vAirway is not 0
then answer "Airway supplies missing from inventory. Please go back and complete Airway section"
else
put "Currently Stocked Items" & cr & fld "iot" card "Send" & cr & cr & "Items Needing Re-stocked" & cr & fld "not" card "Send" into vBody
mobileComposeMail "Test E-mail", someEmail@verizon.net,,, vBody, tAttachment
end if
end if
end if
end if
end mouseUp
Thanks guys for the input....I had tried to write this as a switch in a number of different ways but was getting some unusual results....I tried to break each statement, but it seemed as if it was still pushing down the cases and firing the default statement. I even tried doing the "or" and making a long statement of each but was still having issues....the "exit mouseUp" trick in the end did it for me......I feel I have gotten good at writing "if/else" and "switch" functions so I was unsure what had actually happend that I wasn't getting the results I wanted....Most of the time when I write programs like the one I'm currently doing, I can't help but ask myself if there is a simpler more elegant way to structure my code. I REALLY wish there was someone down the street from me that worked in LC and could mentor me!!! Alas, I will keep pecking on the keyboard and reading until that day comes!!!
Global vTSZero, vWhiteZero, …
on openCard
put false into vTSZero
put false into vWhiteZero
…
end openCard
on mouseUp
if vTS is 0 then
put true into vTSZero
else
answer "Trauma Supplies missing from inventory. Please go back and complete Trauma section"
end if
if vWhite is 0 then
put true into vWhiteZero
else
answer "ALS supplies missing from inventory. Please go back and complete ALS section"
end if
…
if vTSZero and vWhiteZero and … then
put "Currently Stocked Items" & cr & fld "iot" card "Send" & cr & cr & "Items Needing Re-stocked" & cr & fld "not" card "Send" into vBody
mobileComposeMail "Test E-mail", someEmail@verizon.net,,, vBody, tAttachment
end if
end mouseUp
This may not be the best way to do it (globals are discouraged in LiveCode) but I am new to LiveCode and this is the way I would have done it elsewhere.
local vTS,vWhite,vMisc
on mouseUp
put 0 into vTS
put 0 into vWhite
put 0 into vMisc
switchIt
end mouseUp
on switchIt
switch
case vTS <> 0
answer "vTS <> 0"
break
case vWhite <> 0
answer "vWhite <> 0"
break
case vMisc <> 0
answer "vMisc <> 0"
break
default
answer "Currently Stocked Items..."
end switch
end switchIt
This works for me.
I use if-then far too often when I should be using switch. Can you tell me what are your results?
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Simon, as promised, here is what I went with in the end. I like this because it "breaks" at each condition, and doesn't run through them all and alert each section that needs completed until it passes the previous condition. I prefer to use a switch in these cases, but after trying multiple ways of doing it, this seemed to work best. Plus, as soon as I find something that works, I move on! No need to fix what ain't broke...Thanks everyone for all your great input...that is what I LOVE about this community....everyone likes to help each other out...in the end, we all learn a few new tricks!
global vEmail,vShift,vCrew,vRig
on touchEnd pld
mobGUIUntouch the long id of me
if vTS is not 0
then
answer "Trauma Supplies missing from inventory. Please go back and complete Trauma section"
exit touchEnd
end if
if vWhite is not 0
then
answer "ALS supplies missing from inventory. Please go back and complete ALS section"
exit touchEnd
end if
if vMisc is not 0
then
answer "Miscellaneous Supplies missing from inventory. Please go back and complete Miscellaneous section"
exit touchEnd
end if
if vAirway is not 0
then
answer "Airway supplies missing from inventory. Please go back and complete Airway section"
exit touchEnd
end if
put fld "defaultMail" group "emailAlert" into vEmail
put vCrew & cr & cr &"Currently Stocked Items" & cr & fld "iot" card "Send" & cr & cr & "Items Needing Re-stocked" & cr & fld "not" card "Send" into vBody
mobileComposeMail vRig && vShift, vEmail,,, vBody, tAttachment