Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


VBScript - Writing Func/Sub that can take 0 or 1 args?

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,56113
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript - Writing Func/Sub that can take 0 or 1 args?
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 3:58:23 AM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
I was wondering if there was some way that I can write a function or sub that can be called with or without arguments?

I specifically want to be able to write a func/sub that can either accept 1 or 0 arguments and then handle that inside the func/sub to respond differently.

I suspect that the best I can do is pass in an empty string, but I'd be happy to hear anyone's wisdom on this?

_____________________________

value(geeks) > value(mundanes)
 
 
Post #: 1
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 4:37:55 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
VBScript does not support the concept of optional args, so you can't explicitly do what you want. On the rare occasion that I have needed to have optional parameters I have handled it with either a dictionary (named parameters essentially) or an array (order specific parameters essentially).

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to humbletech99)
 
 
Post #: 2
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 4:39:23 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi humbletech99,

the VBScript Docs list "ParamArray" as "VBS feature not supported in VBScript". So
you can't define a function, method, or sub that can be called with a different number
of arguments.

If you use one (or more) scalar variable(s) in your argument list, consider using
Empty for 'unused' parameter(s); e.g.

   doWhatIWant Empty 
   doWhatIWant ""
   doWhatIWant "asap"

Another strategy is to pass one array - or dictionary - to the sub/function/method; e.g.

   sRes = sprintf( "no %% in here!", Array() )
   sRes = sprintf( "%d %s in here!", Array(1, "string" ) )

For a "zero or one" szenario, even defining two functions/subs/methods, would be
worth of discussion too.

Regards

ehvbs

(in reply to humbletech99)
 
 
Post #: 3
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 5:05:00 AM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
could you actually get away with defining two functions of the same name to try to overload the function name and catch it whether it is called with 1 arg or no args?

_____________________________

value(geeks) > value(mundanes)

(in reply to ehvbs)
 
 
Post #: 4
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 5:07:55 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
VBScript does not support overloading either.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to humbletech99)
 
 
Post #: 5
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 5:10:27 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
I wrote:

For a "zero or one" szenario, even defining two functions/subs/methods, would be
worth of discussion too.

but wanted to say:

For a "zero or one" szenario, even defining two functions/subs/methods with
different names
(of course), would be worth of discussion too.

(in reply to ebgreen)
 
 
Post #: 6
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 5:17:26 AM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
well it's 4 functions that I want to behave like this so I'd rather not suddenly have 8 functions with significant redundant code....

I actually thought you were talking about function name overloading...

ps. I think you meant to spell that "scenario"?

_____________________________

value(geeks) > value(mundanes)

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 5:32:18 AM   
  Rischip


Posts: 502
Score: 2
Joined: 3/26/2007
Status: offline
One way to simulate overloading is to send in a single argument which is delimited.
Have the routine split on the delimiter and count the number of arguments.
you could internally "typecast" the indivdual variables if necessary based on position.
It might be difficult to figure out if you are trying to send object as arguments, but simple strings and numbers should be easy.


      

< Message edited by Rischip -- 1/30/2008 5:33:31 AM >


_____________________________

Rischip
Author of - The Grim Linker

(in reply to humbletech99)
 
 
Post #: 8
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 5:39:16 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Good point Rischip. I had forgotten delimited strings. The only reason that I would prefer an array or dictionary over a delimited string is that it is more generalized since I don't have to worry about the delimiter being in one of the arguements and throwing everything out of whack.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Rischip)
 
 
Post #: 9
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 5:46:32 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi humbletech99,

code:


      

output:


      

Regards

ehvbs

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 5:58:52 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Or as one function using an array:

      


_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ehvbs)
 
 
Post #: 11
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 6:04:14 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Rischip,

for some scenarios - e.g. when the parameters are short strings - writing them
in one literal string or joining them is ok. But given other circumstances - e.g.
when you need many arguments of different types - VBScript's lack of serialization/
loss of type information is a serious drawback of your method.

The dictionary approach would give some kind of named parameters (no need to
memorize the order of arguments). But setting up the dictionary is clumsy.

So I think, putting the arguments in an array is the solution for most seasons.

Regards

ehvbs

(in reply to ehvbs)
 
 
Post #: 12
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 6:07:16 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Here is the one function dictionary:


      

As ehvbs pointed out, the setup for the dictionary method is trickier than the array. The array is the method that I most often use, however if the data being passed into the function is sufficiently complex I think the dictionary methods has the potential to be more clear.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ebgreen)
 
 
Post #: 13
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 6:20:03 AM   
  Rischip


Posts: 502
Score: 2
Joined: 3/26/2007
Status: offline
I agree delimited may not be the answer, but what I was going for was a single argument that could be parsed.
A dictionary has one drawback which is it only stores unique items. So it could be painful to assign and retrieve items.

_____________________________

Rischip
Author of - The Grim Linker

(in reply to ebgreen)
 
 
Post #: 14
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 6:24:10 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Could you elaborate on this?

"A dictionary has one drawback which is it only stores unique items. So it could be painful to assign and retrieve items. "

I'm not sure that I understand.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Rischip)
 
 
Post #: 15
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 6:29:21 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Just for completeness, the delimited string method:


      

Notice that the delimited string method is really the same as the array method with the array creation moved into the function.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ebgreen)
 
 
Post #: 16
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 6:32:34 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Rischip,

I think "unique items" is a pro of the dictionary approach; compare ebgreen's

dicArgs.RemoveAll
dicArgs.Add "Txt", "I will never write 'szenario' again!"
...
a second
dicArgs.Add "Txt", "and now something completely different!"
will throw an error
  ...
If dicIn.Exists("Fnd") And dicIn.Exists("Rpl") Then
    improveEN = replace( dicIn("Txt"), dicIn("Fnd"), dicIn("Rpl") )

vs:

args = Split(argument,",",-1,1)
If 2 = UBound( args ) Then
    improveEN = replace( args( 0 ), args( 1 ), args( 2 ) )

It's much more explicit/lucid/safe - but you pay for this feature. Sometimes your
method may well be more suitable.

Regards

ehvbs

(in reply to Rischip)
 
 
Post #: 17
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 6:33:34 AM   
  Rischip


Posts: 502
Score: 2
Joined: 3/26/2007
Status: offline
dicArgs.Add item(0) must be unique as it is the key. So any routine creating the argument to pass would have to account for this.
I think in this instance the Array method would be best, as it can hold objects which would be (how should I say) impossible with a delimited string.

_____________________________

Rischip
Author of - The Grim Linker

(in reply to ebgreen)
 
 
Post #: 18
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 6:42:45 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi ebgreen,

I think your

  Notice that the delimited string method is really the same as the array method with the array creation moved into the function.

is easily misunderstood.

  array creation <> splitting a string into an array
   all datatypes <> strings only
  can be nested <> is flat

Regards

ehvbs

(in reply to Rischip)
 
 
Post #: 19
 
 RE: VBScript - Writing Func/Sub that can take 0 or 1 args? - 1/30/2008 6:42:56 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
I agree that I usually use the array method just due to it's simplicity.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Rischip)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript - Writing Func/Sub that can take 0 or 1 args? Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts