Page 1 of 1

Switch in function - return? -- SOLVED --

Posted: Sat Nov 08, 2008 11:09 pm
by AndyP
I have the following switch:

-- tempext is a 3 char extension

switch (tempext)
case exe
launch templaunch
break
case txt
launch document templaunch
break
case doc
launch document templaunch
break
case com
launch url templaunch
break
case uk
launch url templaunch
break
case net
launch url templaunch
break
case tml
launch url templaunch
break
case php
launch url templaunch
break

end switch

I have 10 buttons I need to apply this to.
How do I put the switch into a function and get the return to perform the relevant launch action?

I know I could get rid of the switch and use if statements and this would be easy to include in a statement but I'm interested if a switch in a function can return?

Posted: Sun Nov 09, 2008 12:20 am
by SparkOut
Top of my head first thought would be to get the function to return the appropriate launch command and then "do" it.

Code: Select all

on mouseUp
   local tempext, templaunch
   answer file "Launch the file..."
   put it into templaunch
   set the itemDelimiter to "."
   put the last item of templaunch into tempext
   -- you'd need to do more error trapping obviously
   do fnLauncher (tempext)
end mouseUp

function fnLauncher pExt
   switch (pExt)
      case "exe" 
         return "launch templaunch"
      break 
      case "txt"
         return "launch document templaunch"
      break 
      case "doc"
         return "launch document templaunch"
      break
      --etc etc 
   end switch
end fnLauncher
A very cursory dry run here shows it working in principle.

Posted: Sun Nov 09, 2008 8:55 am
by Janschenkel
One other thing about 'switch' that you may find interesting is the "fall through" priciple: unless you put a 'break' there, the code of the next 'case' is also executed; also handy is the 'default' case to capture anything you haven't explicitly coded. So your code can look something like this.

Code: Select all

switch tempext
case "exe"
  launch tempLaunch
  break
case "txt"
case "doc"
  launch document templaunch
  break
default
  launch url templaunch
  break
end switch
HTH,

Jan Schenkel.

Posted: Sun Nov 09, 2008 9:47 pm
by AndyP
Thanks SparkOut, can see how this works.

Cheers Jan, did not know about the fall through, tidies things up nicely.

Always great replies and help on the Revolution forum. I used to use Real Basic and was always 'fighting' for information.

Hopefully I will be in a position to help other posters in the near futue.