ErosOlmi
18-03-2007, 09:15
Below post was taken from codingmonkeys (http://www.codingmonkeys.com) forum where Petr posted this nice example on how to use CALL statement for advanced dynamic function calling.
_________________________________________________
Hi,
I'd like to present you one of features which is present in thinBASIC for longer timer, but can be easily over-looked.
Imagine situtation, when you need to call various functions, depending on some condition.
For example, when you want to load some data from various file formats.
So first you will create multiple functions to handle each format:
FUNCTION Load3DS( FileToLoad AS STRING ) AS LONG
' Processing code here
END FUNCTION
FUNCTION LoadOBJ( FileToLoad AS STRING ) AS LONG
' Processing code here
END FUNCTION
FUNCTION LoadM15( FileToLoad AS STRING ) AS LONG
' Processing code here
END FUNCTION
.
.
.
Now you can handle this in thinBASIC like:
IF FileExtension$ = "3DS" THEN
Load3DS( FileName$ )
ELSEIF FileExtension$ = "OBJ" THEN
LoadOBJ( FileName$ )
ELSEIF FileExtension$ = "M15" THEN
LoadM15( FileName$ )
.
.
.
END IF
Lot of typing, isn't it?
So what about:
SELECT CASE FileExtension$
CASE "3DS"
Load3DS( FileName$ )
CASE "OBJ"
LoadOBJ( FileName$ )
CASE "M15"
LoadM15( FileName$ )
.
.
.
END SELECT
This looks better, probably executes even faster ...
But what about doing all this in one line ?
As you can see, I named the functions like "Load<FormatExtension>".
So there is nothing simplier than taking advantage of powerfull CALL keyword:
CALL "Load"+FileExtension$( FileName$ ) To ReturnValue
... simple isn't it ? thinBASIC resolves the name of function at run time and no need for multiline IFs or even SELECT case.
In case the function doesn't exists, thinBASIC produces run-time error and informs you about the problem.
Bye,
Petr
_________________________________________________
Hi,
I'd like to present you one of features which is present in thinBASIC for longer timer, but can be easily over-looked.
Imagine situtation, when you need to call various functions, depending on some condition.
For example, when you want to load some data from various file formats.
So first you will create multiple functions to handle each format:
FUNCTION Load3DS( FileToLoad AS STRING ) AS LONG
' Processing code here
END FUNCTION
FUNCTION LoadOBJ( FileToLoad AS STRING ) AS LONG
' Processing code here
END FUNCTION
FUNCTION LoadM15( FileToLoad AS STRING ) AS LONG
' Processing code here
END FUNCTION
.
.
.
Now you can handle this in thinBASIC like:
IF FileExtension$ = "3DS" THEN
Load3DS( FileName$ )
ELSEIF FileExtension$ = "OBJ" THEN
LoadOBJ( FileName$ )
ELSEIF FileExtension$ = "M15" THEN
LoadM15( FileName$ )
.
.
.
END IF
Lot of typing, isn't it?
So what about:
SELECT CASE FileExtension$
CASE "3DS"
Load3DS( FileName$ )
CASE "OBJ"
LoadOBJ( FileName$ )
CASE "M15"
LoadM15( FileName$ )
.
.
.
END SELECT
This looks better, probably executes even faster ...
But what about doing all this in one line ?
As you can see, I named the functions like "Load<FormatExtension>".
So there is nothing simplier than taking advantage of powerfull CALL keyword:
CALL "Load"+FileExtension$( FileName$ ) To ReturnValue
... simple isn't it ? thinBASIC resolves the name of function at run time and no need for multiline IFs or even SELECT case.
In case the function doesn't exists, thinBASIC produces run-time error and informs you about the problem.
Bye,
Petr