PDA

View Full Version : subroutine parameters


digit
06-18-2009, 02:01 PM
Got a question about optional parameters to subroutines. For a few of my commonly-used subs, I'd like to have some optional parameters to make them a little extensible.

For boolean parameters to say "don't do that" or "do this", probably using a script build condition that checks for the existence of the macro as well as its value can solve that problem. (Though now that I think about it, this kind of behavior is bad programming practice... oops.)

However, for other parameters such as strings to put in the log, things seem a bit more tricky. The best I can come up with is setting the parameter to some default value if the parameter is not defined as the first step of the subroutine. The issue with this is then "popping" that value at the end by deleting the macro.

Is there an example or recommended way of doing this? Or am I trying to do too much? :)

Sorry if this was unclear, and let me know if I can explain anything better.

Thanks in advance.

kinook
06-18-2009, 02:11 PM
Create project or global script functions like this (sample is VBScript):' return expanded macro value if macro is defined, otherwise
' return an empty string
Function MacroOrEmpty(name)
Set m = vbld_AllMacros()(name)
If m Is Nothing Then
MacroOrEmpty = ""
Else
MacroOrEmpty = Application.ExpandMacrosAndScript(m.Value)
End If
End Function

' return expanded macro value as true/false if macro is defined, otherwise
' return False
Function MacroBool(name)
Set m = vbld_AllMacros()(name)
If m Is Nothing Then
MacroBool = False
Else
MacroBool = CBool(Application.ExpandMacrosAndScript(m.Value))
End If
End Function

' ...
then call from the subroutine as needed:

[MacroOrEmpty("XYZ")]

[MacroBool("ABC")]

...

http://www.kinook.com/VisBuildPro/Manual/scripteditor.htm
http://www.kinook.com/VisBuildPro/Manual/script.htm

digit
06-18-2009, 02:26 PM
Good idea on MacroOrEmpty. I should have thought of that.

Thanks!