VBScript doesn't support optional parameters. But if you absolutely must have them, you can use the fact that javascript allows them to your advantage. A really interesting side effect of this is that you end up being able to do something that you can't do by itself in either language: skip one optional parameter and supply a later one!
This example kept getting bigger and bigger because there kept being more things to test. I think I've shown most of how it works, now:
I do not know what the speed implications are of cross-language scripting like this. But the other methods I saw on the web of duplicating optional parameters sucked, namely using a class to pass everything in, or passing all parameters in a string which the receiving function had to parse.
Here is the output of the above if you save it as an .asp file and browse to it:
You can also use Vartype() = 0 instead of IsEmpty() if you like.
I hope this is useful to someone! If so, I'd appreciate you letting me know by posting in this thread.
This example kept getting bigger and bigger because there kept being more things to test. I think I've shown most of how it works, now:
Code:
<%Option Explicit%>
<HTML>
<SCRIPT language="javascript" runat="server">
function myOptionalFunc(requiredParam, optionalParam1, optionalParam2) {
return MyVBFunc(requiredParam, optionalParam1, optionalParam2)
}
function myOptionalFuncWithJSDefault(requiredParam, optionalParam1, optionalParam2) {
return MyVBFunc(
requiredParam,
(typeof optionalParam1 == "undefined") ? "JS Default 1" : optionalParam1,
(typeof optionalParam2 == "undefined") ? "JS Default 2" : optionalParam2
)
}
</SCRIPT>
<%
Function MyVBFunc(RequiredParam, optionalParam1, optionalParam2)
MyVBFunc = Iif(IsEmpty(optionalParam1), "Empty", Iif(VarType(optionalParam1) = 10, "Error", "Present")) & ", "
MyVBFunc = MyVBFunc & Iif(IsEmpty(optionalParam2), "Empty", Iif(VarType(optionalParam2) = 10, "Error", "Present")) & " :
"
If Vartype(optionalParam1) = 10 Then optionalParam1 = "VB Default 1" 'Vartype 10 = vbError
If Vartype(optionalParam2) = 10 Then optionalParam2 = "VB Default 2"
MyVBFunc = MyVBFunc & """" & RequiredParam & """, """ & optionalParam1 & """, """ & optionalParam2 & """"
End Function
Function Iif(Condition, IfTrue, IfFalse)
If Condition then Iif = IfTrue Else Iif = IfFalse
End Function
Response.Write myOptionalFunc("No optional parameters") & "<BR>"
Response.Write myOptionalFunc("one optional parameter", "Optional") & "<BR>"
Response.Write myOptionalFunc("Using keyword undefined", undefined, "abc") & "<BR>"
Response.Write myOptionalFunc("Skipping a parameter", , "Optional") & "<BR>"
Response.Write myOptionalFuncWithJSDefault("JSDefault") & "<BR>"
Response.Write myOptionalFuncWithJSDefault("JSDefault", , "Optional") & "<BR>"
Response.Write myOptionalFuncWithJSDefault("JSDefault", , undefined) & "<BR>"
%>
</HTML>
Here is the output of the above if you save it as an .asp file and browse to it:
Code:
Empty, Empty : "No optional parameters", "", ""
Present, Empty : "one optional parameter", "Optional", ""
Empty, Present : "Using keyword undefined", "", "abc"
Error, Present : "Skipping a parameter", "VB Default 1", "Optional"
Present, Present : "JSDefault", "JS Default 1", "JS Default 2"
Error, Present : "JSDefault", "VB Default 1", "Optional"
Error, Present : "JSDefault", "VB Default 1", "JS Default 2"
I hope this is useful to someone! If so, I'd appreciate you letting me know by posting in this thread.