Does anyone know of a way to duplicate VB's Choose function.
Thanks

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


Re: Choose function by Joe

Joe
Fri Aug 01 11:08:02 CDT 2003

Hi,

"Alexander Mueller" <millerax@hotmail.com> wrote in message
news:bgdngp$5c4$03$1@news.t-online.com...
> 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:

Just a quick note.

Though undocumented VBS does handle *omitted* arguments (all positions
preserved and final argument must be included) identically in both
user-defined functions and Array. (credit: Michael Harris).

---
function TestFn1 (rx1, rx2, rx3, rx4)
TestFn1= "argument 1 is " _
& typename(rx1) _
& " " & vartype(rx1) & vbCr _
& "argument 2 is " _
& typename(rx2) _
& " " & vartype(rx2) & vbCr _
& "argument 3 is " _
& typename(rx3) _
& " " & vartype(rx3) & vbCr _
& "argument 4 is " _
& typename(rx4) _
& " " & vartype(rx4) & vbCr _
end function
---

With a bit more work, *optional* arguments (so long as at least one is
listed) can be simulated by structuring and calling with the Array function.

---
'6-argument function
wscript.echo TestFn2(array(1,,,""))

function TestFn2 (rxArg)
dim vxArg(5)
for viArg= 0 to ubound(rxArg)
vxArg(viArg)= rxArg(viArg)
next
for viArg= 0 to 5
sList= sList & "argument " & (viArg +1) & " is " _
& typename(vxArg(viArg)) _
& " " & vartype(vxArg(viArg)) & vbCr
next: TestFn2= sList
end function
---

Regards,
Joe Earnest




Re: Choose function by Joe

Joe
Fri Aug 01 11:12:43 CDT 2003

Oops, first example should be:

---
'4-argument function
wscript.echo TestFn1(1,,,"")

function TestFn1 (rx1, rx2, rx3, rx4)
TestFn1= "argument 1 is " _
& typename(rx1) _
& " " & vartype(rx1) & vbCr _
& "argument 2 is " _
& typename(rx2) _
& " " & vartype(rx2) & vbCr _
& "argument 3 is " _
& typename(rx3) _
& " " & vartype(rx3) & vbCr _
& "argument 4 is " _
& typename(rx4) _
& " " & vartype(rx4) & vbCr _
end function




Re: Choose function by Joe

Joe
Fri Aug 01 12:54:18 CDT 2003

Hi,

[snipped]
"Alexander Mueller" <millerax@hotmail.com> wrote in message
news:bge8ai$1q8$07$1@news.t-online.com...
> on 01.08.03 18:08 Joe Earnest wrote:
> That's, as I already mentioned, a workaround, but still results in a
different
> signature, for you first have to pack your choices into the array.
>
> JScript supports the va_arg-mechanism precisely

Agreed, and I intended no issue with your response. I was just noting that
if you can use the omitted arguments and Array function together, since the
Array function operates in the same omitted-argument manner as do
user-defined procedures.

Regards,
Joe Earnest




Re: Choose function by Alexander

Alexander
Fri Aug 01 13:48:15 CDT 2003

on 01.08.03 19:54 Joe Earnest wrote:

Hi, Joe

> "Alexander Mueller" wrote:

[omitted, optional & variying arglist]

>> on 01.08.03 18:08 Joe Earnest wrote:
>> That's, as I already mentioned, a workaround, but still results in a
> different
>> signature, for you first have to pack your choices into the array.
>>
>> JScript supports the va_arg-mechanism precisely
>
> Agreed, and I intended no issue with your response. I was just noting that
> if you can use the omitted arguments and Array function together, since the
> Array function operates in the same omitted-argument manner as do
> user-defined procedures.

The point with the omitted args is very intersting indeed.
Since English is not my native tongue, i am not to good
in reading the "tone" of a written message.
Didn't mean to bother either.


Regards,
Alex