Switch statements within Switch statements

Kay C Lan lan.kc.macmail at gmail.com
Wed Jul 9 23:59:42 EDT 2008


On Thu, Jul 10, 2008 at 10:15 AM, Charles Szasz <cszasz at mac.com> wrote:

>
> I have an option Menu that show various arrays of buttons depending on
> which
> menu item is selected. Is it possible to have switch statements for the
> buttons within switch statements for each menu item?


If I understand your situation it is like this:

If you select the 1st choice in your 'Master' option menu you show 'slave'
option menus 1, 2 and 3.
If you select the 2nd choice in your 'Master' option menu you show 'slave'
option menu a,b, and c.
if you select the 3rd choice in your 'Master' option menu you show 'slave'
option menu  1,b,3,d
...

For the scripts in your 'slave' option menus you want different actions
based on not only what selection is made, but what selection was made with
the 'Master' option menu.

If I've got that correct, and assuming that your 'slave' buttons have many
options so you want to keep the switch statement compact, then the script in
your 'slave' buttons could look something like this:

on menuPick pChosen
  switch pChosen
    case "1 Choice"
       --avoid statements here to keep switch statement compact
      handler1Choice
    break
     case "2 Choice"
       handler2Choice
    break
    ...
    ...
     case "987 Choice"
       handler987Choice
     break
    --always good to include in the development phase to hi-light omissions.
    default
      answer "A case I've not accounted for." titled "Case Omission"
      --check the variable watcher - pChosen
      breakpoint
  end switch
end menuPick

--the following will also be in the button script

on handler1Choice
  switch the label of btn "Master"
    case "Choice 1"
       --statements here
       --use a handler if too many statements
     break
    case "Choice 2"
        --statements or handler
     break
     ...
     ...
    case "Choice 99"
       --statements or handler
    break
    --always good to include in the development phase to hi-light omissions.
    default
      answer "A case I've not accounted for." titled "Case Omission"
      --check the label of btn 'Master'
      breakpoint
  end switch
end handler1Choice

on handler2Choice
  --switch structure again
end handler2Choice

as many as required.

Although nested switch statements are possible, and are easy enough to
follow if you only have a small matrix of options to cover, once the options
start to multiply it's very easy to created a very complex structure that is
hard to follow and easy to miss options - which is why I like to use the
default statement to catch omissions.

HTH



More information about the use-livecode mailing list