Archive for category VBScript

Detecting missing “optional” parameters in VBScript

A bit of nostalgia here- We have some VBScript’s in the system I work on which allow users to modify functionality in the system, and we a number of support functions available to support them. Some of the functions have optional parameters which users pass through with no content- for example in a function called “demo” a user might call “demo 1 , , 5″ leaving the middle parameter blank. This is all fine, unless demo calls another function trying to pass these parameters, in which case you get an error. Turns out it’s actually quite hard to detect these missing parameters- i tried the obvious stuff like testing against Null and Nothing and Empty etc etc but they all broke. In the end I had to use the TypeName() function and test to see if “Error” was returned! Bad times- but here’s the code incase anyone else has to do this…

demo "hi", "shaw" , "test"
demo "hi", , "test"
Sub demo(a, b, c)
  MsgBox(VarType(b))
  if TypeName(b) = "Error" then
    b = ""
  end if
  MsgBox(a & " " & b & " " & c)
End Sub

No Comments