Is there ways to create optional arguments to functions in vbscript?

后端 未结 5 1259
名媛妹妹
名媛妹妹 2020-12-17 08:14

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

myFunc(\"happy\")
myFunc(\"happy\", 1,         


        
相关标签:
5条回答
  • 2020-12-17 08:41

    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
    
    0 讨论(0)
  • 2020-12-17 08:48

    How about overloading?

    0 讨论(0)
  • 2020-12-17 08:52

    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.

    0 讨论(0)
  • 2020-12-17 09:00

    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>
    
    0 讨论(0)
  • 2020-12-17 09:03

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

    maybe this helps: http://www.4guysfromrolla.com/webtech/071801-1.shtml

    0 讨论(0)
提交回复
热议问题