Byref

Discussion in 'ASP General' started by Logico, Apr 28, 2005.

  1. Logico

    Logico Guest

    Hi everybody, I've tried to use the byref keyword for passing
    arguments to subroutines and functions in my ASP pages with VBScript,
    but it seems that both byref and byval are irrilevant, as simple
    variables and arrays are always passed by value and objects (I tried
    dictionary ones) are always passed by reference. Is it correct? Or I'm
    wrong and I did a mistake on my test code (that you can find below)?
    Can someone explain to me why there is this behaviour? Then, where is
    the "byref" used? Thanks,

    Alessio

    <%
    Sub square(byval num)
    num=num*num
    end Sub
    Sub incrementa(byref num)
    num=num+100
    end Sub

    Sub squareA(byval a)
    dim k
    for k=lbound(a) to ubound(a)
    a(k) = a(k) * a(k)
    next
    end Sub
    Sub incrementaA(byref a)
    dim k
    for k=lbound(a) to ubound(a)
    a(k) = a(k) + 10
    next
    end Sub

    Sub squareD(byval d)
    dim k
    for each k in d
    d.item(k) = d.item(k) * d.item(k)
    next
    end Sub
    Sub incrementaD(byref d)
    dim k
    for each k in d
    d.item(k) = d.item(k) + 10
    next
    end Sub


    dim b
    b=5
    Response.Write("b=" & b & "#<br>")
    square(b)
    Response.Write("b=" & b & "#<br>")
    incrementa(b)
    Response.Write("b=" & b & "#<br>")


    dim arr
    arr=Array(5, 7, 10)
    Response.Write("arr(1)=" & arr(1) & "#<br>")
    squareA(arr)
    Response.Write("arr(1)=" & arr(1) & "#<br>")
    incrementaA(arr)
    Response.Write("arr(1)=" & arr(1) & "#<br>")


    dim dict, i
    set dict=server.CreateObject("Scripting.Dictionary")
    for i=1 to 3
    dict.Add "K" & i, i
    next
    Response.Write("dict.item('K2')=" & dict.item("K2") & "#<br>")
    squareD(dict)
    Response.Write("dict.item('K2')=" & dict.item("K2") & "#<br>")
    incrementaD(dict)
    Response.Write("dict.item('K2')=" & dict.item("K2") & "#<br>")
    set dict=nothing
    %>
     
    Logico, Apr 28, 2005
    #1
    1. Advertising

  2. Logico

    Mark Schupp Guest

    IIRC the default is ByRef. I normally specify byref or byval explicitly both
    to be sure and as a documentation aid. Objects and arrays may ignore appear
    to always be passed ByRef because the variable is a reference to the object.
    I haven't tried to experiment with this but you could try passing an object
    byval and set the parameter to another object inside the sub. Then see if it
    changes.

    You did not include the output of your test script so there is no way to see
    if it behaves as expected. Try something simple first like:

    sub byvalue( byval n )
    n = 2
    end sub

    sub byreference( byref n )
    n = 3
    end sub

    dim n
    n = 1
    Response.write "n=" & CStr(n) & "<br>"

    call byvalue(n)
    Response.write "n=" & CStr(n) & "<br>"

    call byreference(n)
    Response.write "n=" & CStr(n) & "<br>"

    You should get:
    n=1
    n=1
    n=3

    --
    Mark Schupp



    "Logico" <> wrote in message
    news:...
    > Hi everybody, I've tried to use the byref keyword for passing
    > arguments to subroutines and functions in my ASP pages with VBScript,
    > but it seems that both byref and byval are irrilevant, as simple
    > variables and arrays are always passed by value and objects (I tried
    > dictionary ones) are always passed by reference. Is it correct? Or I'm
    > wrong and I did a mistake on my test code (that you can find below)?
    > Can someone explain to me why there is this behaviour? Then, where is
    > the "byref" used? Thanks,
    >
    > Alessio
    >
    > <%
    > Sub square(byval num)
    > num=num*num
    > end Sub
    > Sub incrementa(byref num)
    > num=num+100
    > end Sub
    >
    > Sub squareA(byval a)
    > dim k
    > for k=lbound(a) to ubound(a)
    > a(k) = a(k) * a(k)
    > next
    > end Sub
    > Sub incrementaA(byref a)
    > dim k
    > for k=lbound(a) to ubound(a)
    > a(k) = a(k) + 10
    > next
    > end Sub
    >
    > Sub squareD(byval d)
    > dim k
    > for each k in d
    > d.item(k) = d.item(k) * d.item(k)
    > next
    > end Sub
    > Sub incrementaD(byref d)
    > dim k
    > for each k in d
    > d.item(k) = d.item(k) + 10
    > next
    > end Sub
    >
    >
    > dim b
    > b=5
    > Response.Write("b=" & b & "#<br>")
    > square(b)
    > Response.Write("b=" & b & "#<br>")
    > incrementa(b)
    > Response.Write("b=" & b & "#<br>")
    >
    >
    > dim arr
    > arr=Array(5, 7, 10)
    > Response.Write("arr(1)=" & arr(1) & "#<br>")
    > squareA(arr)
    > Response.Write("arr(1)=" & arr(1) & "#<br>")
    > incrementaA(arr)
    > Response.Write("arr(1)=" & arr(1) & "#<br>")
    >
    >
    > dim dict, i
    > set dict=server.CreateObject("Scripting.Dictionary")
    > for i=1 to 3
    > dict.Add "K" & i, i
    > next
    > Response.Write("dict.item('K2')=" & dict.item("K2") & "#<br>")
    > squareD(dict)
    > Response.Write("dict.item('K2')=" & dict.item("K2") & "#<br>")
    > incrementaD(dict)
    > Response.Write("dict.item('K2')=" & dict.item("K2") & "#<br>")
    > set dict=nothing
    > %>
     
    Mark Schupp, Apr 28, 2005
    #2
    1. Advertising

  3. Logico wrote:
    > Hi everybody, I've tried to use the byref keyword for passing
    > arguments to subroutines and functions in my ASP pages with VBScript,
    > but it seems that both byref and byval are irrilevant, as simple
    > variables and arrays are always passed by value and objects (I tried
    > dictionary ones) are always passed by reference. Is it correct? Or I'm
    > wrong and I did a mistake on my test code (that you can find below)?
    > Can someone explain to me why there is this behaviour? Then, where is
    > the "byref" used? Thanks,
    >
    > squareD(b)


    Using parentheses when passing an argument forces the argument to be passed
    byval (simplified explanation). Do not use parentheses when calling a sub:

    squareD b

    For more information, see here:

    http://blogs.msdn.com/ericlippert/archive/2003/09/15/52996.aspx

    Bob Barrows

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
     
    Bob Barrows [MVP], Apr 28, 2005
    #3
  4. Logico

    StephenMcC Guest

    Hey,

    I have a another query on this issue, when using ByRef parameters,
    espiceally in ASP with VBScript, is it more expensive to use ByRef than ByVal
    params...ie: as below...

    sOutPut = MyFunction(Data1, Data2)

    Public Function MyFunction(ByRef Param1, ByRef Param2)
    some code
    ...
    End Sub

    ....if the Param1 & Param2 are not going to be changed inside the function
    call then should I drop the use of ByRef in the function definition and pass
    the params ByVal: 'sOutPut = MyFunction((Data1), (Data2))' , might this help
    save memory/resources, espiceally if this call is used many times in a single
    page?

    Any insight into this would be most helpful, any links even better!

    Cheers.

    StephenMcC
    ..



    "Bob Barrows [MVP]" wrote:

    > Logico wrote:
    > > Hi everybody, I've tried to use the byref keyword for passing
    > > arguments to subroutines and functions in my ASP pages with VBScript,
    > > but it seems that both byref and byval are irrilevant, as simple
    > > variables and arrays are always passed by value and objects (I tried
    > > dictionary ones) are always passed by reference. Is it correct? Or I'm
    > > wrong and I did a mistake on my test code (that you can find below)?
    > > Can someone explain to me why there is this behaviour? Then, where is
    > > the "byref" used? Thanks,
    > >
    > > squareD(b)

    >
    > Using parentheses when passing an argument forces the argument to be passed
    > byval (simplified explanation). Do not use parentheses when calling a sub:
    >
    > squareD b
    >
    > For more information, see here:
    >
    > http://blogs.msdn.com/ericlippert/archive/2003/09/15/52996.aspx
    >
    > Bob Barrows
    >
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.
    >
    >
    >
     
    StephenMcC, May 9, 2005
    #4
  5. Logico

    Evertjan. Guest

    =?Utf-8?B?U3RlcGhlbk1jQw==?= wrote on 09 mei 2005 in
    microsoft.public.inetserver.asp.general:

    > I have a another query on this issue, when using ByRef parameters,
    > espiceally in ASP with VBScript, is it more expensive to use ByRef
    > than ByVal params...ie: as below...
    >
    > sOutPut = MyFunction(Data1, Data2)
    >
    > Public Function MyFunction(ByRef Param1, ByRef Param2)
    > some code
    > ...
    > End Sub
    >


    Very expensive.
    As this is not part of vbscript under ASP,
    you will be fined with an error.


    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)
     
    Evertjan., May 9, 2005
    #5
  6. Logico

    StephenMcC Guest

    Hi Evertjan,

    Thanks for ur speedy reply. Sorry but I'm at a loss, do u mean the function
    declaration will cause an error !? This is how we are currently doing it and
    it doesn't complain. I think I may change the code to pass ByVal where no
    return is required, in the call to the function droping parentheses around
    the params not to be returned.

    StephenMcC
    ..

    "Evertjan." wrote:

    > =?Utf-8?B?U3RlcGhlbk1jQw==?= wrote on 09 mei 2005 in
    > microsoft.public.inetserver.asp.general:
    >
    > > I have a another query on this issue, when using ByRef parameters,
    > > espiceally in ASP with VBScript, is it more expensive to use ByRef
    > > than ByVal params...ie: as below...
    > >
    > > sOutPut = MyFunction(Data1, Data2)
    > >
    > > Public Function MyFunction(ByRef Param1, ByRef Param2)
    > > some code
    > > ...
    > > End Sub
    > >

    >
    > Very expensive.
    > As this is not part of vbscript under ASP,
    > you will be fined with an error.
    >
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Replace all crosses with dots in my emailaddress)
    >
    >
     
    StephenMcC, May 9, 2005
    #6
  7. Evertjan. wrote:
    > =?Utf-8?B?U3RlcGhlbk1jQw==?= wrote on 09 mei 2005 in
    > microsoft.public.inetserver.asp.general:
    >
    >> I have a another query on this issue, when using ByRef parameters,
    >> espiceally in ASP with VBScript, is it more expensive to use ByRef
    >> than ByVal params...ie: as below...
    >>
    >> sOutPut = MyFunction(Data1, Data2)
    >>
    >> Public Function MyFunction(ByRef Param1, ByRef Param2)
    >> some code
    >> ...
    >> End Sub
    >>

    >
    > Very expensive.
    > As this is not part of vbscript under ASP,
    > you will be fined with an error.
    >
    >

    ?
    Are you talking about "End Sub" instead of "End Function"?

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
     
    Bob Barrows [MVP], May 9, 2005
    #7
  8. StephenMcC wrote:
    > Hey,
    >
    > I have a another query on this issue, when using ByRef parameters,
    > espiceally in ASP with VBScript, is it more expensive to use ByRef
    > than ByVal params...ie: as below...
    >


    Since a copy of the variable is made, yes ByVal is a little more expensive.
    However, I sincerely doubt that anyone would ever notice the difference. I
    tend to use ByVal unless ByRef functionality s needed.
    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
     
    Bob Barrows [MVP], May 9, 2005
    #8
  9. "StephenMcC" <> wrote in message
    news:...
    > Hey,
    >
    > I have a another query on this issue, when using ByRef parameters,
    > espiceally in ASP with VBScript, is it more expensive to use ByRef than
    > ByVal
    > params...ie: as below...
    >
    > sOutPut = MyFunction(Data1, Data2)
    >
    > Public Function MyFunction(ByRef Param1, ByRef Param2)
    > some code
    > ...
    > End Sub
    >
    > ...if the Param1 & Param2 are not going to be changed inside the function
    > call then should I drop the use of ByRef in the function definition and
    > pass
    > the params ByVal: 'sOutPut = MyFunction((Data1), (Data2))' , might this
    > help
    > save memory/resources, espiceally if this call is used many times in a
    > single
    > page?
    >
    > Any insight into this would be most helpful, any links even better!


    It depends on the size of the parameters. ByRef will incur very minor
    overhead dereferencing the ByRef parameter, so I only use it for longs and
    integers if I need the callee to be able to alter the value.

    Otoh, assuming ByVal causes a copy to be made, it would be more expensive in
    terms of both cpu and memory to pass a large string by value.

    Objects cannot be passed by value in the classic sense, the ByVal modifier
    causes something else to happen, (that never seemed intuitive to me, and I
    can't seem to find any docs about it.)


    -Mark





    > Cheers.
    >
    > StephenMcC
    > .
    >
    >
    >
    > "Bob Barrows [MVP]" wrote:
    >
    >> Logico wrote:
    >> > Hi everybody, I've tried to use the byref keyword for passing
    >> > arguments to subroutines and functions in my ASP pages with VBScript,
    >> > but it seems that both byref and byval are irrilevant, as simple
    >> > variables and arrays are always passed by value and objects (I tried
    >> > dictionary ones) are always passed by reference. Is it correct? Or I'm
    >> > wrong and I did a mistake on my test code (that you can find below)?
    >> > Can someone explain to me why there is this behaviour? Then, where is
    >> > the "byref" used? Thanks,
    >> >
    >> > squareD(b)

    >>
    >> Using parentheses when passing an argument forces the argument to be
    >> passed
    >> byval (simplified explanation). Do not use parentheses when calling a
    >> sub:
    >>
    >> squareD b
    >>
    >> For more information, see here:
    >>
    >> http://blogs.msdn.com/ericlippert/archive/2003/09/15/52996.aspx
    >>
    >> Bob Barrows
    >>
    >> --
    >> Microsoft MVP -- ASP/ASP.NET
    >> Please reply to the newsgroup. The email account listed in my From
    >> header is my spam trap, so I don't check it very often. You will get a
    >> quicker response by posting to the newsgroup.
    >>
    >>
    >>
     
    Mark J. McGinty, May 9, 2005
    #9
  10. Logico

    Patrice Guest

    Objects are basically a pointer. Using ByVal you pass a pointer, using ByRef
    a pointer to a pointer. In both cases you can reach the object that is then
    updatable. Additionay using ByRef you can assign a new object to the pointer
    and reflect this change in the caller...

    I'm not talking specifically about VBScript (could have specific things as
    it uses the "variant" datatype) but rather about VB /VB.NET...

    Patrice
    --

    "Mark J. McGinty" <> a écrit dans le message de
    news:%...
    >
    > "StephenMcC" <> wrote in message
    > news:...
    > > Hey,
    > >
    > > I have a another query on this issue, when using ByRef parameters,
    > > espiceally in ASP with VBScript, is it more expensive to use ByRef than
    > > ByVal
    > > params...ie: as below...
    > >
    > > sOutPut = MyFunction(Data1, Data2)
    > >
    > > Public Function MyFunction(ByRef Param1, ByRef Param2)
    > > some code
    > > ...
    > > End Sub
    > >
    > > ...if the Param1 & Param2 are not going to be changed inside the

    function
    > > call then should I drop the use of ByRef in the function definition and
    > > pass
    > > the params ByVal: 'sOutPut = MyFunction((Data1), (Data2))' , might this
    > > help
    > > save memory/resources, espiceally if this call is used many times in a
    > > single
    > > page?
    > >
    > > Any insight into this would be most helpful, any links even better!

    >
    > It depends on the size of the parameters. ByRef will incur very minor
    > overhead dereferencing the ByRef parameter, so I only use it for longs and
    > integers if I need the callee to be able to alter the value.
    >
    > Otoh, assuming ByVal causes a copy to be made, it would be more expensive

    in
    > terms of both cpu and memory to pass a large string by value.
    >
    > Objects cannot be passed by value in the classic sense, the ByVal modifier
    > causes something else to happen, (that never seemed intuitive to me, and I
    > can't seem to find any docs about it.)
    >
    >
    > -Mark
    >
    >
    >
    >
    >
    > > Cheers.
    > >
    > > StephenMcC
    > > .
    > >
    > >
    > >
    > > "Bob Barrows [MVP]" wrote:
    > >
    > >> Logico wrote:
    > >> > Hi everybody, I've tried to use the byref keyword for passing
    > >> > arguments to subroutines and functions in my ASP pages with VBScript,
    > >> > but it seems that both byref and byval are irrilevant, as simple
    > >> > variables and arrays are always passed by value and objects (I tried
    > >> > dictionary ones) are always passed by reference. Is it correct? Or

    I'm
    > >> > wrong and I did a mistake on my test code (that you can find below)?
    > >> > Can someone explain to me why there is this behaviour? Then, where is
    > >> > the "byref" used? Thanks,
    > >> >
    > >> > squareD(b)
    > >>
    > >> Using parentheses when passing an argument forces the argument to be
    > >> passed
    > >> byval (simplified explanation). Do not use parentheses when calling a
    > >> sub:
    > >>
    > >> squareD b
    > >>
    > >> For more information, see here:
    > >>
    > >> http://blogs.msdn.com/ericlippert/archive/2003/09/15/52996.aspx
    > >>
    > >> Bob Barrows
    > >>
    > >> --
    > >> Microsoft MVP -- ASP/ASP.NET
    > >> Please reply to the newsgroup. The email account listed in my From
    > >> header is my spam trap, so I don't check it very often. You will get a
    > >> quicker response by posting to the newsgroup.
    > >>
    > >>
    > >>

    >
    >
     
    Patrice, May 10, 2005
    #10
  11. "Patrice" <> wrote in message
    news:%...
    > Objects are basically a pointer. Using ByVal you pass a pointer, using
    > ByRef
    > a pointer to a pointer. In both cases you can reach the object that is
    > then
    > updatable. Additionay using ByRef you can assign a new object to the
    > pointer
    > and reflect this change in the caller...
    >
    > I'm not talking specifically about VBScript (could have specific things as
    > it uses the "variant" datatype) but rather about VB /VB.NET...


    That makes sense.

    What is the default for objects, any idea? Guessing from observation, it
    *looks* like it's ByRef for both objects and variants of object sub-type in
    VB, but always ByVal in VBS?

    Thanks,
    Mark




    > Patrice
    > --
    >
    > "Mark J. McGinty" <> a écrit dans le message de
    > news:%...
    >>
    >> "StephenMcC" <> wrote in message
    >> news:...
    >> > Hey,
    >> >
    >> > I have a another query on this issue, when using ByRef parameters,
    >> > espiceally in ASP with VBScript, is it more expensive to use ByRef than
    >> > ByVal
    >> > params...ie: as below...
    >> >
    >> > sOutPut = MyFunction(Data1, Data2)
    >> >
    >> > Public Function MyFunction(ByRef Param1, ByRef Param2)
    >> > some code
    >> > ...
    >> > End Sub
    >> >
    >> > ...if the Param1 & Param2 are not going to be changed inside the

    > function
    >> > call then should I drop the use of ByRef in the function definition and
    >> > pass
    >> > the params ByVal: 'sOutPut = MyFunction((Data1), (Data2))' , might this
    >> > help
    >> > save memory/resources, espiceally if this call is used many times in a
    >> > single
    >> > page?
    >> >
    >> > Any insight into this would be most helpful, any links even better!

    >>
    >> It depends on the size of the parameters. ByRef will incur very minor
    >> overhead dereferencing the ByRef parameter, so I only use it for longs
    >> and
    >> integers if I need the callee to be able to alter the value.
    >>
    >> Otoh, assuming ByVal causes a copy to be made, it would be more expensive

    > in
    >> terms of both cpu and memory to pass a large string by value.
    >>
    >> Objects cannot be passed by value in the classic sense, the ByVal
    >> modifier
    >> causes something else to happen, (that never seemed intuitive to me, and
    >> I
    >> can't seem to find any docs about it.)
    >>
    >>
    >> -Mark
    >>
    >>
    >>
    >>
    >>
    >> > Cheers.
    >> >
    >> > StephenMcC
    >> > .
    >> >
    >> >
    >> >
    >> > "Bob Barrows [MVP]" wrote:
    >> >
    >> >> Logico wrote:
    >> >> > Hi everybody, I've tried to use the byref keyword for passing
    >> >> > arguments to subroutines and functions in my ASP pages with
    >> >> > VBScript,
    >> >> > but it seems that both byref and byval are irrilevant, as simple
    >> >> > variables and arrays are always passed by value and objects (I tried
    >> >> > dictionary ones) are always passed by reference. Is it correct? Or

    > I'm
    >> >> > wrong and I did a mistake on my test code (that you can find below)?
    >> >> > Can someone explain to me why there is this behaviour? Then, where
    >> >> > is
    >> >> > the "byref" used? Thanks,
    >> >> >
    >> >> > squareD(b)
    >> >>
    >> >> Using parentheses when passing an argument forces the argument to be
    >> >> passed
    >> >> byval (simplified explanation). Do not use parentheses when calling a
    >> >> sub:
    >> >>
    >> >> squareD b
    >> >>
    >> >> For more information, see here:
    >> >>
    >> >> http://blogs.msdn.com/ericlippert/archive/2003/09/15/52996.aspx
    >> >>
    >> >> Bob Barrows
    >> >>
    >> >> --
    >> >> Microsoft MVP -- ASP/ASP.NET
    >> >> Please reply to the newsgroup. The email account listed in my From
    >> >> header is my spam trap, so I don't check it very often. You will get a
    >> >> quicker response by posting to the newsgroup.
    >> >>
    >> >>
    >> >>

    >>
    >>

    >
    >
     
    Mark J. McGinty, May 10, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Goncalo

    ByVal and ByRef

    Goncalo, Dec 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    6,087
    Dmitriy Lapshin [C# / .NET MVP]
    Dec 4, 2003
  2. Alex Brown

    StateServer pass byref threading problem

    Alex Brown, Apr 21, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    345
    Alex Brown
    Apr 22, 2004
  3. Iams

    Byval vs. byref

    Iams, Feb 24, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    4,805
  4. xamman
    Replies:
    1
    Views:
    345
    Marina Levit [MVP]
    Dec 22, 2006
  5. Rob Meade

    Byref / Byval?

    Rob Meade, Dec 28, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    773
    Rob Meade
    Dec 28, 2006
Loading...

Share This Page