PDA

View Full Version : Sub routine Tables



Michael Clease
20-12-2007, 15:44
I was thinking about creating a table which contains a list of sub/Functions routines.

Is that possible? I wanted to avoid having multiple IF's or using SELECT CASE, this should make script execution faster and smaller.

Pseudo Code...
In the example I create the table of routines then check a value, this value is also my offset for my table, it then calls the routine.
All that with one check.


DIM Routines(3) AS DWORD

Routines(1) = Test1,Test2,Test3

IF ButtonClicked > 0 THEN CALL Routines(ButtonClicked)

SUB Test1()
... Do something ...
END SUB

SUB Test2()
... Do something ...
END SUB

SUB Test3()
... Do something ...
END SUB

ErosOlmi
20-12-2007, 15:55
Should work but with string. See following:



DIM Routines(3) AS string
dim ButtonClicked as long = 1
dim FunctionToCall as string

Routines(1) = "Test1", "Test2", "Test3"


IF ButtonClicked > 0 THEN
FunctionToCall = Routines(ButtonClicked) '---Compose the name as preferred
CALL FunctionToCall
end if

SUB Test1()
msgbox 0, "Test 1"
END SUB

SUB Test2()
msgbox 0, "Test 2"
END SUB

SUB Test3()
msgbox 0, "Test 3"
end sub


Ciao
Eros

ErosOlmi
20-12-2007, 16:15
You can also have parameters if you need some:



randomize

dim ButtonClicked as long = rnd(1, 3)
dim FunctionToCall as string


IF ButtonClicked > 0 THEN
FunctionToCall = "Test" & ButtonClicked '---Compose the name as preferred
CALL FunctionToCall(timer)
end if

SUB Test1(optional byval MyParam as ext)
msgbox 0, "Test 1" & $crlf & "MyParam equal to: " & MyParam
END SUB

SUB Test2(optional byval MyParam as long)
msgbox 0, "Test 2"
END SUB

SUB Test3(optional byval MyParam as long)
msgbox 0, "Test 3"
end sub

Michael Clease
20-12-2007, 16:32
thanks Eros I will test when I get home tonight.

ErosOlmi
20-12-2007, 16:38
OK, perfect.
While I was checking ... worth to mention also function_exists utility. So above example become:


randomize

dim ButtonClicked as long = rnd(1, 5)
dim FunctionToCall as string


IF ButtonClicked > 0 THEN
FunctionToCall = "Test" & ButtonClicked '---Compose the name as preferred

'---Check if function name exists. If yes, call it, otherwise error.
if function_exists(FunctionToCall) then
CALL FunctionToCall(timer)
else
msgbox 0, FunctionToCall & " does not exists."
end if
end if

SUB Test1(optional byval MyParam as ext)
msgbox 0, "Test 1" & $crlf & "MyParam equal to: " & MyParam
END SUB

SUB Test2(optional byval MyParam as long)
msgbox 0, "Test 2"
END SUB

SUB Test3(optional byval MyParam as long)
msgbox 0, "Test 3"
end sub


Ciao
Eros

Michael Clease
21-12-2007, 16:52
thanks works perfect for me, seems quite obvious how to do it now I see your examples.

ErosOlmi
21-12-2007, 16:57
Good.

I will implement a bit CALL (http://www.thinbasic.com/public/products/thinBasic/help/html/call.htm) help. It is not clear immediately the power of this statement and the sequence it uses to decide what to call.
The string expression containing the name of the function to call can contain any function name: script user function, declared API name functions, thinBasic keywords. Just a bit of attention must be taken into considerations about parameters.

Ciao
Eros