30

Is there ways to create optional arguments to functions in vb script allowing you to write functions something like...

myFunc("happy")
myFunc("happy", 1, 2, 3)
myFunc("happy", 1)
etc.
Rob Segal
  • 7,515
  • 12
  • 43
  • 72

5 Answers5

18

The optional keyword (like in VB6) is not allowed in vbscript

maybe this helps: https://web.archive.org/web/20210304114036/http://www.4guysfromrolla.com/webtech/071801-1.shtml

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
Daniel Kreiseder
  • 12,135
  • 9
  • 38
  • 59
  • Sort of. Ideally I was hoping to avoid using an array or null arguments supplied to the function. Guess it isn't possible with VB script. Thanks for the help. – Rob Segal Dec 11 '09 at 20:48
  • Hi ! Please can you edit your link because i didn't find it ? may be is moved to another page or link. Thank You ! – Hackoo Jun 14 '22 at 22:48
  • https://web.archive.org/web/20210304114036/http://www.4guysfromrolla.com/webtech/071801-1.shtml – Daniel Kreiseder Jun 17 '22 at 11:24
3

How about overloading?

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
3

You can always make it a class and use Public Property Let to populate your Sub/Function before calling it:

  Set oSubName = New cSubName

    'fill your parameters, you can always add more later
    oClass.OptionalParameter1 = true
    oClass.OptionalParameter2 = false

    'execute sub
    oSubName.Execute

  Set oSubName = Nothing

This would require some knowledge on how to make classes, but is probably the next best solution to using arrays.

Good luck.

Control Freak
  • 12,965
  • 30
  • 94
  • 145
1

Obviosly this depends on your environment and whether it supports using both JScript and VBScript in the same file, but I've had some success using Windows Script Host (*.wsf files), ie

<?xml version="1.0" standalone="yes" ?>
<package xmlns="Windows Scripting Host">
    <job id="param">
    <?job debug="true"?>

        <script language="JavaScript">
            <![CDATA[
                function MakeString(args) {
                    var s = [];
                    for(var i = 0, length = arguments.length; i < length; i++)
                        s.push(arguments[i]);
                    return s.join('');
                }
            ]]>
        </script>
        <script language="vbscript">
            <![CDATA[
                WScript.Echo MakeString("hello", " ", "world")
                WScript.Echo MakeString()
                WScript.Echo MakeString(1,2,3,4)
            ]]>
        </script>
    </job>
</package>

where you can define your function in JScript and reference it in VBScript. A better way may well be to include your JScript functions as an external file ie

    <script language="JavaScript" src="makestring.js"/>
    <script language="vbscript">
        <![CDATA[
            WScript.Echo MakeString("hello", " ", "world")
            WScript.Echo MakeString()
            WScript.Echo MakeString(1,2,3,4)
        ]]>
    </script>
-5

Just pass a different type and look it.

wscript.echo "1: " & math(7,8,false)
wscript.echo "2: " & math(7,8,5)
wscript.echo "3: " & math(15,false,5)

function math( Addend1 , Addend2, Divisor )
dim tmpTotal

if Addend2 then
   TmpTotal = Addend1 + Addend2
else
   tmpTotal = Addend1
end if


if Divisor then     'if argument Divisor is other than False then do it.
   if Divisor > 0 then  'Hated Divide by Zero since VIC-20.
      tmpTotal = tmpTotal / Divisor)
   end if
end if

end function
Darin
  • 1