Optional parameters for function and sub 
Author Message
 Optional parameters for function and sub

Dear All,
How can i keep a parameter optional in a function or sub. I am facing a big
problem while adding a new parameter to a sub, because i have to make
changes everywhere i m using this sub..
Any help would be appreciated.

Thanks
Regards,
Hakim S.



Wed, 07 Dec 2005 14:24:18 GMT  
 Optional parameters for function and sub
Optional arguments are not supported in VB Script.  Here's one of many pages
about that that may help you get around this though.
http://www.4guysfromrolla.com/webtech/071801-1.shtml

Ray at home


Quote:
> Dear All,
> How can i keep a parameter optional in a function or sub. I am facing a
big
> problem while adding a new parameter to a sub, because i have to make
> changes everywhere i m using this sub..
> Any help would be appreciated.

> Thanks
> Regards,
> Hakim S.



Wed, 07 Dec 2005 14:46:04 GMT  
 Optional parameters for function and sub
Hi,

Since you're posting in .VBScript, I'll assume that you're talking about
VBScript.  VBScript does not allow optional parameters on user-defined
procedures, in the sense that the entire parameter (comma and all) can be
omitted.  While undocumented, VBScript does allow omitted arguments in all
but the final position, but the positions must be preserved, and a final
argument must be stated.  (Credit Michael Harris for the info.)

E.g, for

function MyFunction (argA, argB, argC)

you can call

xRtn= MyFunction(,, xValue)

but not

xRtn= MyFunction(xValue1, xValue2).

This ability probably doesn't help you, as I read your post, since you
appear to want to leave the same number of arguments already called before
the modification.  FWIW, an omitted argument is passed as an Error type and
can be tested with IsError.

As an alternative, can you rewrite the sub to use an existing argument in an
alternative way, instead of just adding one?  For example, if you already
have an optional argument passed as an integer, can it do double duty and
allow either an integer or a string and use VarType or TypeName to check on
what is passed?  Or can you change a required integer argument to accept
both a single integer or an integer array, and if an array is passed,
internally assign all the elements?

E.g.,

function MyFunction (arg1)
dim vnInt1, vnInt2
if (vartype(arg1)=vbInteger) then vnInt1= arg1 _
   else if (vartype(arg1)>=vbArray) then _
   vnInt1= arg1(0): vnInt2= arg1(1)

Joe Earnest


Quote:
> Dear All,
> How can i keep a parameter optional in a function or sub. I am facing a
big
> problem while adding a new parameter to a sub, because i have to make
> changes everywhere i m using this sub..
> Any help would be appreciated.

> Thanks
> Regards,
> Hakim S.



Thu, 08 Dec 2005 01:40:41 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Optional parameters for function and sub

2. manipulating files in memory

3. Need Help with Program

4. How to call a sub with Optional Parameter??!

5. Declaring a Sub with optional Parameters

6. Optional Argument in Sub or Function

7. how to set optional parameters in the vbscript function(asp pages)

8. Functions with optional parameters?

9. Optional Parameters in a WSH Function

10. * ASP / VBScript - Optional parameters in functions

11. Optional Function Parameters

12. Optional function parameters

 

 
Powered by phpBB® Forum Software