ByRef or not ByRef, that is the array? 
Author Message
 ByRef or not ByRef, that is the array?

Is there a better way of passing and returning an array other than using
"ByRef" in the sub/function parameters.

e.g.
    Sub MyFunction(ByRef array1)
        .
        code
        .
    End Sub

I've tried

    Sub MyFunction(array1)
        .
        code
        .
        MyFunction = array1
    End Sub

But I get Type Mismatch errors, so I take it this isn't the way to do it.
:o)



Mon, 27 Oct 2003 00:26:19 GMT  
 ByRef or not ByRef, that is the array?
I now just discovered that ByRef only works with sub if you use Call.  An
error message is only displayed when requesting a sub procedure with 2 or
more parameters and no Call prefixes it....

eg
Sub MyProc()
    Blah code Blah
End sub

Sub MyProc2(para)
    Blah code Blah
End sub

Sub MyProc3(para1, para2)
    Blah code Blah
End sub

MyProc() ' is ok
MyProc(para) ' is ok, but if the parameter paused ByRef it doesn't work
MyProc(para, para) ' causes error.

Why is vbs so non-consistent?  Give me Bash or sh any day, there is more
consistency between these too shells then one vbs version!


Quote:
> Is there a better way of passing and returning an array other than using
> "ByRef" in the sub/function parameters.

> e.g.
>     Sub MyFunction(ByRef array1)
>         .
>         code
>         .
>     End Sub

> I've tried

>     Sub MyFunction(array1)
>         .
>         code
>         .
>         MyFunction = array1
>     End Sub

> But I get Type Mismatch errors, so I take it this isn't the way to do it.
> :o)



Mon, 27 Oct 2003 19:33:27 GMT  
 ByRef or not ByRef, that is the array?
On Thu, 10 May 2001 12:33:27 +0100 in
microsoft.public.scripting.VBScript, "Pritchie.com"

Quote:

>I now just discovered that ByRef only works with sub if you use Call.  An
>error message is only displayed when requesting a sub procedure with 2 or
>more parameters and no Call prefixes it....
[...]
>MyProc() ' is ok
>MyProc(para) ' is ok, but if the parameter paused ByRef it doesn't work
>MyProc(para, para) ' causes error.

>Why is vbs so non-consistent?  Give me Bash or sh any day, there is more
>consistency between these too shells then one vbs version!

That's not quite true. The problem is the parentheses, which aren't
necessary within the reference to the sub.

MyProc para

will pass the parameter by reference, if that's what the sub
specifies.

Enlosing a variable in parentheses causes it to be evaluated, thus the
value of the variable is passed rather than the variable itself.

hth
--
Adam D. Barratt

Please reply to the newsgroup rather than via e-mail



Mon, 27 Oct 2003 20:09:16 GMT  
 ByRef or not ByRef, that is the array?
Ok, I understand that, but, what I don't like is, a) it defaults to ByRef
and b) why can you call it with, without brackets, and with and without the
call command?  I found that I use brackets because it makes it easy for
myself to read the code (from a Java point of view), so therefore I now also
use the Call command all the time so I get consistency.  I suppose it's just
me learning the language.  :o(



Quote:
> On Thu, 10 May 2001 12:33:27 +0100 in
> microsoft.public.scripting.vbscript, "Pritchie.com"

> >I now just discovered that ByRef only works with sub if you use Call.  An
> >error message is only displayed when requesting a sub procedure with 2 or
> >more parameters and no Call prefixes it....
> [...]
> >MyProc() ' is ok
> >MyProc(para) ' is ok, but if the parameter paused ByRef it doesn't work
> >MyProc(para, para) ' causes error.

> >Why is vbs so non-consistent?  Give me Bash or sh any day, there is more
> >consistency between these too shells then one vbs version!

> That's not quite true. The problem is the parentheses, which aren't
> necessary within the reference to the sub.

> MyProc para

> will pass the parameter by reference, if that's what the sub
> specifies.

> Enlosing a variable in parentheses causes it to be evaluated, thus the
> value of the variable is passed rather than the variable itself.

> hth
> --
> Adam D. Barratt

> Please reply to the newsgroup rather than via e-mail



Mon, 27 Oct 2003 22:46:27 GMT  
 ByRef or not ByRef, that is the array?
It's a VB/VBA/VBScript thing ;-)...

Check the docs for any of those 3 languages for the distinction between a Sub and a Function and the
() requirements and also the Call statement.  It's easier to follow with examples with more than one
argument...

MySub arg1,arg2   <=== normal sub call
MySub(arg1),(arg2)   <=== passes value of expressions
MySub(arg1,arg2)   <=== invalid syntax - can't use ()'s when calling a sub as a sub
Call MySub(arg1,arg2)   <=== calling a sub as a function via explicit Call statement
result = MyFunc(arg1,arg2)   <=== normal function call
result = MyFunc((arg1),(arg2))   <=== normal function call passing value of expressions
Call MyFunc(arg1,arg2)   <=== discarding function return value
MyFunc arg1,arg2   <=== calling a function "sub-style" discarding function return value

--
Michael Harris
Microsoft.MVP.Scripting
--

Please do not email questions - post them to the newsgroup instead.
--

Quote:

> Ok, I understand that, but, what I don't like is, a) it defaults to ByRef
> and b) why can you call it with, without brackets, and with and without the
> call command?  I found that I use brackets because it makes it easy for
> myself to read the code (from a Java point of view), so therefore I now also
> use the Call command all the time so I get consistency.  I suppose it's just
> me learning the language.  :o(



> > On Thu, 10 May 2001 12:33:27 +0100 in
> > microsoft.public.scripting.vbscript, "Pritchie.com"

> > >I now just discovered that ByRef only works with sub if you use Call.  An
> > >error message is only displayed when requesting a sub procedure with 2 or
> > >more parameters and no Call prefixes it....
> > [...]
> > >MyProc() ' is ok
> > >MyProc(para) ' is ok, but if the parameter paused ByRef it doesn't work
> > >MyProc(para, para) ' causes error.

> > >Why is vbs so non-consistent?  Give me Bash or sh any day, there is more
> > >consistency between these too shells then one vbs version!

> > That's not quite true. The problem is the parentheses, which aren't
> > necessary within the reference to the sub.

> > MyProc para

> > will pass the parameter by reference, if that's what the sub
> > specifies.

> > Enlosing a variable in parentheses causes it to be evaluated, thus the
> > value of the variable is passed rather than the variable itself.

> > hth
> > --
> > Adam D. Barratt

> > Please reply to the newsgroup rather than via e-mail



Tue, 28 Oct 2003 03:01:56 GMT  
 ByRef or not ByRef, that is the array?
Thanks Micheal, once again I just copy & pasted your good example below.  I
bet you've got a whole load of these archived somewhere on your local drive
ready for instance use when silly newbies like me ask the same questions
again and again... Yeah I know I am creeping, but hey, I what to keep on
your good side yeah? :o)

something I did notice, which I found annoying when I didn't know better, is
the vbs is happy with say calling a sub using {MySub (MyParameter)}, but not
with {MySub (MyParameter, MyParameter2)} unless you include a Call, because
of the reasons discribed below.  This is ok when you know why (or how VB
does these things), but when you don't it can be very dangerous and none
intuitive.  I mean, I read and thought that by using ByRef and ByVal I could
controlled how my Sub or Function operates, however, I now know that as an
author I cannot force a contract of operation on the way my functions and
precedures handles parameters.  Ahh, but maybe I am just having too high an
expectations for VB consistancy and it 'user friendlyness'.  I mean its had
a hard time growing from QBasic.....  hehehehe... soz all you VB fans...
Hey I like it too, in a 'its better than CMD way' (even if it does take 5 x
time more code to do somethings :o)


Quote:
> It's a VB/VBA/VBScript thing ;-)...

> Check the docs for any of those 3 languages for the distinction between a

Sub and a Function and the
Quote:
> () requirements and also the Call statement.  It's easier to follow with

examples with more than one
Quote:
> argument...

> MySub arg1,arg2   <=== normal sub call
> MySub(arg1),(arg2)   <=== passes value of expressions
> MySub(arg1,arg2)   <=== invalid syntax - can't use ()'s when calling a sub
as a sub
> Call MySub(arg1,arg2)   <=== calling a sub as a function via explicit Call
statement
> result = MyFunc(arg1,arg2)   <=== normal function call
> result = MyFunc((arg1),(arg2))   <=== normal function call passing value
of expressions
> Call MyFunc(arg1,arg2)   <=== discarding function return value
> MyFunc arg1,arg2   <=== calling a function "sub-style" discarding function
return value

> --
> Michael Harris
> Microsoft.MVP.Scripting
> --

<snip>


Tue, 28 Oct 2003 23:54:57 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Property Let argument ByRef is NOT ByRef ?

2. sub examplefunc(byref var1,byref var2,byref var2) doesnt work!

3. pass array to dll ByRef

4. How to pass vbscript array variable to shared object using ByRef

5. passing a byref array

6. Passing arrays byref

7. How to pass control array ByRef?

8. Passing a control array byRef ??

9. pass array to dll ByRef

10. pass array to dll ByRef

11. passing a control array byref

12. Arrays byref

 

 
Powered by phpBB® Forum Software