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

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   humbletech99 

  • Group: Members
  • Posts: 4
  • Joined: 08-July 07

Posted 30 January 2008 - 10:58 AM

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?


#2 User is online   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,118
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 30 January 2008 - 11:31 AM

What about something like this?
set Args = Wscript.Arguments
If Args.Count < 1 Then
  Do Something
Else do differently


<Edit>
I've just re-read your request and your looking for arguments to a function or sub.

There is no method for this only workarounds. Being primarily a batch man my suggestion would be to pass an array and use logic to allocate variables from there.
Example:
somesub Array(13, 23, somevar)

  sub somesub(aArgs)
	if IsArray(aArgs) then
	  x = aArgs(0)
	  if Ubound(aArgs) > 0 y = aArgs(1)
	  if Ubound(aArgs) > 1 a = aArgs(2)
	  if Ubound(aArgs) > 2 b = aArgs(3)
	Else
	  x = aArgs
	End if
  End sub
</Edit>

#3 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 30 January 2008 - 11:37 AM

With not much info here is a demo I made for you.
Save as Arg.vbs

Quote

 Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
 Dim DelFile
 If Wscript.Arguments.Count = 0 Then
  WScript.Echo "This Needs A File To Be Drop" & vbCrLf & _
			   "And Drop On To This Script."
 Else
  For Each DelFile in Wscript.Arguments : Fso.DeleteFile(DelFile) : Next
 End If


#4 User is offline   humbletech99 

  • Group: Members
  • Posts: 4
  • Joined: 08-July 07

Posted 30 January 2008 - 11:46 AM

Sorry guys, you seem to have misunderstood me, I'm not talking about WScript.Arguments as in arguments to the script itself, but actually a function within a script that I'd like to be able to call with either one parameter, "a message string", or no parameters, in which case the function would behave slightly differently.

I suspect that I cannot call a func/sub defined with one arg without any args and that I must pass an empty string to do this

#5 User is offline   SilverBulletUK 

  • Quite likes the Command Line, but not with chips you understand.
  • Pip
  • Group: Members
  • Posts: 59
  • Joined: 27-December 04

Posted 16 February 2008 - 12:34 PM

Well, with PHP you can do the following....
<?php
function someFunction($messageString = null)
{
	if($messageString != null)
	{
		 //Do something
	}
	else
	{
		 //Do something else with user defined $messageString
	}
}
?>


Is this not possible? Maybe by specifing a default value?

#6 User is offline   humbletech99 

  • Group: Members
  • Posts: 4
  • Joined: 08-July 07

Posted 19 February 2008 - 04:20 AM

err, that's that question, can you do something like that in VBScript?

So far, I haven't found an answer, you have to pass in the predefined number of parameters to every function call (unless you pass in an array), you can't have it take 0 or 1 parameters it seems.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy