[quoted text, click to view] 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.
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
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 [quoted text, click to view] "Logico" <amilani@dsi.unive.it> wrote in message news:ffe0d3c1.0504280644.17733dd5@posting.google.com... > 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 > %>
=?Utf-8?B?U3RlcGhlbk1jQw==?= wrote on 09 mei 2005 in microsoft.public.inetserver.asp.general: [quoted text, click to view] > 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)
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 .. [quoted text, click to view] "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. > >
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 .. [quoted text, click to view] "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) >
[quoted text, click to view] 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.
[quoted text, click to view] 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.
[quoted text, click to view] "StephenMcC" <StephenMcC@discussions.microsoft.com> wrote in message news:38813C7B-F2D4-4ECF-B526-00EFA22E7A45@microsoft.com... > 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 [quoted text, click to view] > 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. >> >> >>
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" <mmcginty@spamfromyou.com> a écrit dans le message de news:%23Pv2nBOVFHA.2700@TK2MSFTNGP12.phx.gbl... [quoted text, click to view] > > "StephenMcC" <StephenMcC@discussions.microsoft.com> wrote in message > news:38813C7B-F2D4-4ECF-B526-00EFA22E7A45@microsoft.com... > > 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. > >> > >> > >> > >
[quoted text, click to view] "Patrice" <nobody@nowhere.com> wrote in message news:%23bN9cRTVFHA.3076@TK2MSFTNGP12.phx.gbl... > 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 [quoted text, click to view] > Patrice > -- > > "Mark J. McGinty" <mmcginty@spamfromyou.com> a écrit dans le message de > news:%23Pv2nBOVFHA.2700@TK2MSFTNGP12.phx.gbl... >> >> "StephenMcC" <StephenMcC@discussions.microsoft.com> wrote in message >> news:38813C7B-F2D4-4ECF-B526-00EFA22E7A45@microsoft.com... >> > 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. >> >> >> >> >> >> >> >> > >
Don't see what you're looking for? Try a search.
|