Re: Choose function by Alexander
Alexander
Fri Aug 01 07:47:49 CDT 2003
on 01.08.03 05:02 Ed wrote:
> Does anyone know of a way to duplicate VB's Choose function.
> Thanks
>
Since VBS lacks the option to define optional params
in your own procedures, you could try JScript:
WScript.Echo (Choose(2,"fred","barny","holofernes"));
function Choose() { //signature: (index, choice1,..,choiceN)
var index, len;
if ((len = arguments.length) < 1) return null;
if (isNaN(index = arguments[0]) || index == 0 || index > len-1)
return null;
return arguments[index];
}
The only way to do define a similar function in VBS is to use an array
for the choices, which results in a different signature/function-call
compared to VB/A.
WScript.Echo Choose(2,Array("fred","barny","holofernes"))
function Choose(index, arrOfChoices)
Choose = Null
if not TypeName(arrOfChoices) = "Variant()" then Exit function
if not isNumeric (index) then Exit function
if index = 0 OR index>Ubound(arrOfChoices)+1 then Exit function
Choose = arrOfChoices(index-1)
end function
--
Gruesse,
Alex