Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optional parameters in ASP VBScript functions 1

Status
Not open for further replies.

ESquared

Programmer
Dec 23, 2003
6,129
US
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:

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>
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:
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"
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.
 
That's good insight, an 'A' (star) for your work!!!

<.
 
Oh... to put it all in perspective, I think it's best (simplest to remember and implement) to just use the top javascript "forwarding" function, and handle the defaults in VBScript. It might be easiest to create your own IsMissing() function or Mz() function, something like:

Code:
<SCRIPT language="javascript" runat="server">
function DoSomething(optionalParam1, optionalParam2, optionalParam3) {
   return DoSomething_VB(optionalParam1, optionalParam2, optionalParam3)
}
</SCRIPT>
<%
Function IsMissing(TheValue)
   If VarType(TheValue) = 0 or VarType(TheValue) = 10 Then
      IsMissing = True
   Else
      IsMissing = False
   End If
End Function

Function Mz(TheValue, ValueIfMissing) 'in honor of VB Nz() function
   If VarType(TheValue) = 0 or VarType(TheValue) = 10 Then
      Mz = ValueIfMissing
   Else
      Mz = TheValue
   End If
End Function

Function DoSomething_VB(optionalParam1, optionalParam2, optionalParam3)
   optionalParam1 = Mz(optionalParam1, 2)
   optionalParam2 = Mz(optionalParam2, 5)
   optionalParam3 = Mz(optionalParam3, 9)
   'Now do stuff with all the parameters
   DoSomething_VB = optionalParam1 * optionalParam2 * optionalParam3
End Function

Response.Write DoSomething( , ,3)
%>
Erik

PS There was a line-break error in the code in my original post, but you can probably figure it out.
 
By the way, one side-effect of this method is that if you call the javascript function with no parameters, you have to use empty parentheses. If you don't, you'll get a string returned which contains the text of the function's script!

Response.Write DoSomething

will return

[ul]function DoSomething(optionalParam1, optionalParam2, optionalParam3) {
return DoSomething_VB(optionalParam1, optionalParam2, optionalParam3)
}[/ul]

You have to do

Response.Write DoSomething()

to get it to work.


Another thing worth mentioning is that you don't have to worry about case sensitivity, either in VBScript or Javascript, when calling across languages. It seems like the translator-handling routines handle this okay, since VBScript is not a case-sensitive language.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top