Author Topic: Call sub by name  (Read 2237 times)

0 Members and 1 Guest are viewing this topic.

Aurel

  • Guest
Call sub by name
« on: September 22, 2011, 10:56:02 AM »
is there a way to call( jump to) subroutine by name(string) ...?

Charles Pegge

  • Guest
Re: Call sub by name
« Reply #1 on: September 22, 2011, 12:53:48 PM »
Hi Aurel,

There are quite a few ways to do it.

This one is efficient for short lists of procedures

Code: OxygenBasic
  1.  
  2.  
  3.   '===========================
  4.  'CALLING FUNCTION BY BY NAME
  5.  '===========================
  6.  
  7.   ' this is okay for short lists of procedures
  8.  
  9.   function fa() label
  10.   print "A"
  11.   end function
  12.  
  13.   function fb() label
  14.   print "B"
  15.   end function
  16.  
  17.   function fc() label
  18.   print "C"
  19.   end function
  20.  
  21.   sys    FunPtr[3]<=(@fa,@fb,@fc)
  22.   string FunList=" 01 fa 02 fb 03 fc "
  23.  
  24.  
  25.  
  26.   function CallByName(string name)
  27.   '===============================
  28.  sys a,b,pf
  29.   a=instr FunList," "+name+" "
  30.   if a then
  31.     b=val mid FunList, a-2,2
  32.     pf=FunPtr(b)
  33.     call pf
  34.   end if
  35.   end function
  36.  
  37.  
  38.   CallByName "fb"
  39.  
  40.  

Charles

Aurel

  • Guest
Re: Call sub by name
« Reply #2 on: September 22, 2011, 03:06:04 PM »
Excellent Charles... ;)
Thanks for example....

Aurel

Peter

  • Guest
Re: Call sub by name
« Reply #3 on: September 22, 2011, 03:31:33 PM »
Hi Charles,

Quote
if a then 

is this a new standard?

Charles Pegge

  • Guest
Re: Call sub by name
« Reply #4 on: September 22, 2011, 07:21:48 PM »

No it was there from the beginning.

In Oxygen you can also do it with string variables. An empty or null string is treated as false, all others are true.

string er
...
if er then print er



Charles