understanding switch and variables

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Scott Richardson
Posts: 34
Joined: Mon May 08, 2006 7:02 pm

understanding switch and variables

Post by Scott Richardson » Mon Sep 13, 2010 10:15 pm

In other projects I have used simple if/then statements, but I now have situation where switch might be better. I have read the documentation again and again, but I still don't quite get it.

My needs are as follows:

I have a main stack (foundation) with a substack (console). All of my buttons, etc. are in the console substack which trigger things in the main stack. I have a button that I want to have show the next field of a set of invisible fields on the current card of the main stack. There are seven cards in the main stack each with different sets of fields, but I want the same button to function for all of them dependent on which card is current.

Here is just one sample of the many that I have tried:

Code: Select all

 on mouseUp
   go to current card of stack "foundation"
   put cardName into Here
   switch Here
      case Here = "one"
   if visible of fld "F1" is true
   then show fld "F2"
   if visible of fld "F1" is false
   then show fld "F1"
break
      case Here = "two"
   if visible of fld "F3" is true
   then show fld "F4"
   if visible of fld "F3" is false
   then show fld "F3"
break  
end switch
end mouseUp 
What am I not understanding? This doesn't give me back any errors, but nothing happens. Can you guys help me better understand "switch," etc.?

Thanks,
Scott

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: understanding switch and variables

Post by Regulae » Mon Sep 13, 2010 11:54 pm

Hi Scott,

I gather that your first aim is to have the variable cardName loaded with the name of the card you’re currently viewing in stack “Foundation”. I don’t think the lines:

Code: Select all

go to current card of stack "foundation"
put cardName into Here
... does quite what you want, that is, put the name of the currently viewed card into the variable Here. If I could have a stab at a solution for you, try putting:

Code: Select all

global cardName

on openCard
   put the short name of this card into cardName
end openCard
... into the stack script of stack “Foundation”. With this in its stack script, every time you go to a different card in the “Foundation” stack, the short name of the card (from your example “one”, “two” and so on) will be “loaded” into the global variable cardName. Now, if you put the following into the script of the button on your console stack:

Code: Select all

global cardName

on mouseUp
   go stack "Foundation"
   switch cardName
      case "one"
         if visible of fld "F1" is true
         then show fld "F2" 
         if visible of fld "F1" is false
         then show fld "F1"
         break
      case "two"
         if visible of fld "F3" is true
         then show fld "F4"
         if visible of fld "F3" is false
         then show fld "F3"
         break 
   end switch
end mouseUp
...as you see, because we are using the global cardName, we don’t really need to load it into the Here variable- we can just use cardName directly in the switch structure. Note also I didn’t say:

Code: Select all

case cardName = "one"
... I know you are thinking in terms of a familiar “if/then” condition- the switch structure simply looks at the variable cardName- in the case when cardName is “one” (i.e. case “one”) flds “F1” and “F2” are referred to, case “two” refers to flds “F3” and “F4” so switch can represent quite complex conditional branching very simply.
Incidentally, I might mention you don’t necessarily have to go to stack “Foundation” (although this might be what you want for your purposes). It’s possible to control what is shown on stack “Foundation” without going to it. In this case you have to tell Rev where the fields are, e.g.:

Code: Select all

global cardName

on mouseUp
   switch cardName
      case "one"
         if visible of fld "F1" of card cardName of stack "Foundation" is true
         then show fld "F2" of card cardName of stack "Foundation"
         if visible of fld "F1" of card cardName of stack "Foundation" is false
         then show fld "F1" of card cardName of stack "Foundation"
         break
      case "two"
         if visible of fld "F3" of card cardName of stack "Foundation" is true
         then show fld "F4" of card cardName of stack "Foundation"
         if visible of fld "F3" of card cardName of stack "Foundation" is false
         then show fld "F3" of card cardName of stack "Foundation"
         break 
   end switch
end mouseUp
... we’ve dropped the “go” command, but instead have given a pretty exact indication of exactly which fields we are referring to. I hope this is in the right direction for you. I have given it a bit of a test, but do ask if you strike any problems. This is perhaps one approach along the lines you require.
Regards,
Michael

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 157
Joined: Thu Jun 29, 2006 4:16 pm

Re: understanding switch and variables

Post by Randy Hengst » Tue Sep 14, 2010 12:20 am

Hello Scott,

Here are two more examples that should do what I think you're looking for.

on mouseUp
go to current card of stack "foundation"
put the short name of current card into Here
switch -- omit the the name of the variable from this line and include it with the case statement
case here = "one" --- or omit the name of the variable in the case statement and include it only with the switch statement
if the visible of fld "F1" is true then
show fld "F2"
end if
if the visible of fld "F1" is false then
show fld "F1"
end if
break
case here = "two"
if visible of fld "F3" is true then
show fld "F4"
end if
if visible of fld "F3" is false then
show fld "F3"
end if
end switch
end mouseUp

on mouseUp
switch short name of this card
case "one"
if the visible of fld "F1" is true then
show fld "F2"
end if
if the visible of fld "F1" is false then
show fld "F1"
end if
break
case "two"
if visible of fld "F3" is true then
show fld "F4"
end if
if visible of fld "F3" is false then
show fld "F3"
end if
end switch
end mouseUp

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: understanding switch and variables

Post by Klaus » Wed Sep 15, 2010 10:41 am

Hi all,

it is always a good idea to also close the last "case" with a "break"!

Code: Select all

...
case "two"
  if visible of fld "F3" is true then
     show fld "F4"
  end if
  if visible of fld "F3" is false then
     show fld "F3"
  end if
BREAK 
end switch
end mouseUp
Best

Klaus

Post Reply