PDA

View Full Version : Problem with returning a string from a function



DavidMead
26-11-2008, 10:30
I have a simple function to return a string variable but I am getting error 252 "invalid delimter" when it returns but I can't see why it should fail.

Is there something I am missing?

FUNCTION Exec_STRTEST() AS STRING

LOCAL strvar AS STRING

strvar = "Return String"

FUNCTION = strvar

END FUNCTION

Michael Hartlef
26-11-2008, 11:58
Yip, looks right to me too. Does your line in LoadLocalSymbols look like this? Pay attention to the %thinBasic_ReturnString




thinBasic_LoadSymbol "Exec_STRTEST",%thinBasic_ReturnString,CodePtr(xExec_STRTEST),%thinBasic_ForceOverWrite

ErosOlmi
26-11-2008, 12:06
Hi David,

attached to this post an example module using your function. Also a thinkbasic script testing the module function STRTEST.

Be sure to:

create your own functions (as you already did)
attach your function to a thinBasic keyword using thinBasic_LoadSymbol API function inside LoadLocalSymbols module function
attention to indicate in thinBasic_LoadSymbol the correct return code (usually %thinBasic_ReturnString or %thinBasic_ReturnNumber)


Let me know.
Ciao
Eros

DavidMead
26-11-2008, 12:39
Thanks for the help. After looking at your program Eros I knew I must be doing something wrong so I went back and had a look and found where I was going wrong.

Originally I was trying to pass some parameters into the function but had the problem so I reduced it to the simple test program and still had the problem.

With the test program and no parameters I was calling the function with empty paranthesis which caused a failure. I should have been calling with no brackets if no parameters.

With the program where I was passing a parameter I forgot to check the thinBasic_CheckCloseParens. When I add that it works ok.

ErosOlmi
26-11-2008, 12:51
Ok, perfect.
Let me know if you need any further help.

Regarding ( and ) parenthesis, to let your syntax to optionally use ( and ), I usually do the following.

Imagine you want a function with the following syntax:
STRTEST (AnyString AS String, AnyNumber as LONG)

you can parse it in this way:


'----------------------------------------------------------------------------
Function Exec_STRTEST() As String
' Simple test function: STRTEST(AnyString, AnyNumber)
'----------------------------------------------------------------------------
Local MyStr As String
Local MyNumber As Ext
Local PP As Long 'pp stands for Parens Present

'---Check if first parens was present and store it
pp = thinBasic_CheckOpenParens_Optional

'---Parse the first param (string)
thinBasic_ParseString MyStr

'---Check the mandatory comma
If thinBasic_CheckComma_Mandatory Then

'---Parse the second param (numeric)
thinBasic_ParseNumber MyNumber

'---If there was a ( than it is mandatory to have a )
If pp Then thinBasic_CheckCloseParens_Mandatory

'---If no error than we can procede
If thinBasic_ErrorFree Then

'--Do something with your data and return something else
Function = Left$(MyStr, MyNumber)
End If

End If

End Function


The above will work for

STRTEST ("Any string", 2)
but also for

STRTEST "Any string", 2
giving more freedom to the thinBasic programmer ;)

Hope it can help.

Ciao
Eros