=====================================================
Part III: Example based on the Recommended UDF Format
=====================================================


Below is an example UDF illustrating the concepts mentioned in Part II. Please study it carefully!
Code:

dim $rc, $a, $b, $c, $d
$rc=SETOPTION('Explicit','On')
$rc=SETOPTION('NoVarsInStrings','On')
$rc=SETOPTION('CaseSensitivity','On')
$a='test'
$b=10
$c='whatever'
$d=11
$rc=UDFTemplate($a,$b,$c,$d)
? 'Error = '+@ERROR+' - '+@SERROR
? 'Result of UDFTemplate:'
? $rc

;FUNCTION UDFTemplateDemo()
;
;ACTION Illustrates best-practices concepts to be used in UDFs
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;CONTRIBUTORS Richard Howarth
;
;VERSION 1.0
;
;DATE CREATED 2003/03/15
;
;DATE MODIFIED 2003/03/17
;
;
;KIXTART KiXtart 4.20
;
;SYNTAX UDFTEMPLATEDEMO(VAR1, VAR2 [, VAR3, VAR4])
;
;PARAMETERS VAR1
; Required string parameter
;
; VAR2
; Required integer parameter
;
; VAR3
; Optional string paarmeter
;
; VAR4
; Optional integer parameter with bit-wise settings:
; 1 - 'Bit 1 is set'
; 2 - 'Bit 2 is set'
; 4 - 'Bit 4 is set'
; 8 - 'Bit 8 is set'
; 16 - 'Bit 16 is set'
;
;RETURNS String containing words
;
;REMARKS Please study carefully the concepts used
;
;DEPENDENCIES none (actually, a brain)
;
;EXAMPLE dim $rc, $a, $b, $c, $d
; SETOPTION('Explicit','On')
; SETOPTION('NoVarsInStrings','On')
; SETOPTION('CaseSensitivity','On')
; $a='test'
; $b=10
; $c='whatever'
; $d=11
; $rc=UDFTemplateDemo($a,$b,$c,$d)
; ? 'Error = '+@ERROR+' - '+@SERROR
; ? 'Result of UDFTemplate:'
; ? $rc
;
;KIXTART BBS http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=12;t=000000
;
function UDFTemplateDemo($var1, $var2, optional $var3, optional $var4)
; DIM all variables used inside the UDF
dim $helpvar, $helparray[4]

; set the default return value
$udftemplate=''

; perform checks on all input parameters to make sure they fit the requirements
; if necessary, exit the UDF and return an error code

; var1 must be a string, thus convert to string if it is not a string
if vartype($var1)<>8
$var1=''+$var1
endif

; var 2 must be an integer, thus convert var2 into an integer
$var2=val($var2)

; var3 must be a string, thus convert to string if it is not a string
; however, it is also an optional parameter, thus we set it to a default value
; if the parameter is not provided
if vartype($var3)
if vartype($var3)<>8
$var3=''+$var3
endif
else
; parameter has not been provided, thus set default value
$var3='default value'
endif

; var 4 must be an integer, if it is provided
if vartype($var4)
if vartype($var4)<>2 and vartype($var4)<>3
; variable is not an integer, thus exit with 'Invalid Parameter' error code
exit 87
endif
else
; parameter has not been provided, thus set default value
$var4=0
endif

? 'Content of variable $var1 = '+$var1
? 'Variable type of $var2 = '+vartype($var2)+' - '+vartypename($var2)
? 'Content of variable $var3 = '+$var3

; demonstrating bit-wise decisions
for $helpvar=0 to 4
if $var4 & ($helpvar+1)
$helparray[$helpvar]='Bit '+($helpvar+1)+' is set'
else
$helparray[$helpvar]='Bit '+($helpvar+1)+' is not set'
endif
next

$udftemplate=join($helparray,@CRLF)

; UDF ran successfully, thus set exit code to 0
; this is optional, however it will reset a potentially
; existing @ERROR back to 0
exit 0
endfunction




Edited by kdyer (2004-02-09 04:51 PM)
_________________________
There are two types of vessels, submarines and targets.