Question on the Function Argument List 
Author Message
 Question on the Function Argument List

Is it possible to make some variables being passed to a function through an
argument list to be optional?

For Example here is the function

Function tstFunct (Var1, Var2, Var3)
  'Manipulate Variables
End Function

I keep getting errors when I try and call the function by passing only the
first variable.

  tstFunct Var1

I realize that it would not be possible to have Var2 optional and make Var3
Required.  I would though like to make Var2 and Var3 optional.

Any Ideas?



Tue, 19 Feb 2002 03:00:00 GMT  
 Question on the Function Argument List
This is not possible in VBScript. You can pass a single Argument,
though.

Function tstFunct(aVar)
    If IsArray(aVar) Then
        'multiple arguments got passed in useing an array
    Else
        'only one argument got passed in, so handle this
    End If
End Function

So you can call
erg = tstFunct("myVar")
or
erg = tstFunct(Array("Var1", "Var2"))
or use any other number of arguments.

HTH, Thomas



Quote:
> Is it possible to make some variables being passed to a function
through an
> argument list to be optional?

> For Example here is the function

> Function tstFunct (Var1, Var2, Var3)
>   'Manipulate Variables
> End Function

> I keep getting errors when I try and call the function by passing
only the
> first variable.

>   tstFunct Var1

> I realize that it would not be possible to have Var2 optional and
make Var3
> Required.  I would though like to make Var2 and Var3 optional.

> Any Ideas?



Tue, 19 Feb 2002 03:00:00 GMT  
 Question on the Function Argument List

You can use an array with empty or null elements for "optional" arguments (as Thomas pointed out) or you can simply pass empty/null "placeholder" values, but you still have to account for all of the arguments.  

As of the current version of VBScript (5.0), the necessary Optional keyword and IsMissing() function aren't supported to get true optional arguments.  Nor is the (somewhat related) ParamArray keyword).  Maybe (hint, hint,..) on a future release?

--
Michael Harris


  Is it possible to make some variables being passed to a function through an
  argument list to be optional?

  For Example here is the function

  Function tstFunct (Var1, Var2, Var3)
    'Manipulate Variables
  End Function

  I keep getting errors when I try and call the function by passing only the
  first variable.

    tstFunct Var1

  I realize that it would not be possible to have Var2 optional and make Var3
  Required.  I would though like to make Var2 and Var3 optional.

  Any Ideas?



Tue, 19 Feb 2002 03:00:00 GMT  
 Question on the Function Argument List

I had an example I forgot to include (duh!)...

tstFunct 1,2,3
tstFunct 1,null,null
tstFunct 1,2,null
tstFunct null,2,null
tstFunct 1,null,3
tstFunct 1,empty,empty
tstFunct 1,2,empty
tstFunct empty,2,empty
tstFunct 1,empty,3

Function tstFunct (Var1, Var2, Var3)
  msgbox typename(var1) & " " _
       & typename(var2) & " " _
       & typename(var3) & vbcrlf _
       & IsNull(var1) & " " _
       & IsNull(var2) & " " _
       & IsNull(var3) & vbcrlf _
       & IsEmpty(var1) & " " _
       & IsEmpty(var2) & " " _
       & IsEmpty(var3)
  if isnull(var2) then
    var2 = "some default"      
    msgbox "ver2 defaulted to " & var2
  end if

End Function

So any argument that tests as IsNull or IsEmpty (depending on which convention you adopt) would be assigned a default value locally (within the function).

--
Michael Harris


  You can use an array with empty or null elements for "optional" arguments (as Thomas pointed out) or you can simply pass empty/null "placeholder" values, but you still have to account for all of the arguments.  

  As of the current version of VBScript (5.0), the necessary Optional keyword and IsMissing() function aren't supported to get true optional arguments.  Nor is the (somewhat related) ParamArray keyword).  Maybe (hint, hint,..) on a future release?

  --
  Michael Harris


    Is it possible to make some variables being passed to a function through an
    argument list to be optional?

    For Example here is the function

    Function tstFunct (Var1, Var2, Var3)
      'Manipulate Variables
    End Function

    I keep getting errors when I try and call the function by passing only the
    first variable.

      tstFunct Var1

    I realize that it would not be possible to have Var2 optional and make Var3
    Required.  I would though like to make Var2 and Var3 optional.

    Any Ideas?



Tue, 19 Feb 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. function argument list...

2. Passing argument list to functions

3. Crazy question about optional function arguments

4. Urgent question:How to define array argument of the function in dll

5. variable size argument list in vbs?

6. Unable to execute - arguments list is too long

7. variable argument list?

8. argument list too long errors

9. Arguments list.

10. Unable to execute - arguments list is too long

11. unable to execute - arguments list is too long...

12. Using list dropdown of constants that pertain to a specific argument

 

 
Powered by phpBB® Forum Software