Switch in function - return? -- SOLVED --

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
AndyP
Posts: 634
Joined: Wed Aug 27, 2008 12:57 pm
Contact:

Switch in function - return? -- SOLVED --

Post by AndyP » Sat Nov 08, 2008 11:09 pm

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?
Last edited by AndyP on Sun Nov 09, 2008 9:48 pm, edited 1 time in total.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Sun Nov 09, 2008 12:20 am

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.

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Sun Nov 09, 2008 8:55 am

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.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

AndyP
Posts: 634
Joined: Wed Aug 27, 2008 12:57 pm
Contact:

Post by AndyP » Sun Nov 09, 2008 9:47 pm

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.

Post Reply