Login | |
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
|
|