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(byre f 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(byr ef 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(byr ef 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.Cre ateObject("Scri pting.Dictionar y")
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(dic t)
Response.Write( "dict.item('K2' )=" & dict.item("K2") & "#<br>")
set dict=nothing
%> 10 4426
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" <am*****@dsi.un ive.it> wrote in message
news:ff******** *************** ***@posting.goo gle.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(byre f 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(byr ef 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(byr ef 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.Cre ateObject("Scri pting.Dictionar y") 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(dic t) Response.Write( "dict.item('K2' )=" & dict.item("K2") & "#<br>") set dict=nothing %>
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/ar.../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.
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(Data 1, Data2)
Public Function MyFunction(ByRe f 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((Dat a1), (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/ar.../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.
=?Utf-8?B?U3RlcGhlbk1 jQw==?= wrote on 09 mei 2005 in
microsoft.publi c.inetserver.as p.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(Data 1, Data2)
Public Function MyFunction(ByRe f 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)
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?U3RlcGhlbk1 jQw==?= wrote on 09 mei 2005 in microsoft.publi c.inetserver.as p.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(Data 1, Data2)
Public Function MyFunction(ByRe f 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. wrote: =?Utf-8?B?U3RlcGhlbk1 jQw==?= wrote on 09 mei 2005 in microsoft.publi c.inetserver.as p.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(Data 1, Data2)
Public Function MyFunction(ByRe f 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.
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.
"StephenMcC " <St********@dis cussions.micros oft.com> wrote in message
news:38******** *************** ***********@mic rosoft.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(Data 1, Data2)
Public Function MyFunction(ByRe f 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((Dat a1), (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/ar.../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" <mm******@spamf romyou.com> a écrit dans le message de
news:%2******** ********@TK2MSF TNGP12.phx.gbl. .. "StephenMcC " <St********@dis cussions.micros oft.com> wrote in message news:38******** *************** ***********@mic rosoft.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(Data 1, Data2)
Public Function MyFunction(ByRe f 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((Dat a1), (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/ar.../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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sandy |
last post by:
Hello!
Help!!!!
I have ten zillion books that attempt to describe the
difference between ByVal and ByRef and none of them are
clear to me. I have gathered that ByVal makes a copy and
ByRef points to something and changes it. The default for
simple data types is ByVal and for objects, it's ByRef.
Am I correct so far? If so, I still don't have a clue as
|
by: Cc |
last post by:
hi,
is there a way to use byref on property set , because i would like to pass
the value into the variable byref ?
|
by: Hei |
last post by:
Hi,
i know the difference of ByRef and ByVal, in case if use byref or byval
don't affect the result which one should prefer? (less memory use, better
performance ....issue)
thx
|
by: Rob Panosh |
last post by:
Hello,
Ok here is the senerio:
.....
Dim myArrayList as New ArrayList(0)
me.Test_A( myArrayList )
|
by: Carlos Gomez |
last post by:
In VB6 the default for passing variables was ByRef. It was faster and
used less memory. Why did MS changed that? Are there any advantages
using ByVal over ByRef? (other than ByVal impeding you from changing
the original variable passed)
| |
by: Robin Tucker |
last post by:
Although I've been working on this project for 8 months now, I'm still not
sure of the difference between ByVal and ByRef. As most objects in VB are
reference types, passing ByVal I've discovered allows me to store a
reference to the object I passed by val, to change that object and for the
change to be reflected in the callers copy of the reference (confused?).
Well, what is byref for in that case?
I'm coming from a C++ background...
|
by: Warren Sirota |
last post by:
Hi,
Please let me know if I am interpreting this correctly. I've done a
little testing of the difference between passing parameters byVal and
byRef, and the results were slightly non-intuitive, as I expected. I
haven't seen this behavior explained precisely in the .net world yet,
so I wanted to check and make sure I've got it right.
I apologize that this is a bit long. I've tried to keep it concise.
There are code fragments here, but...
|
by: barrett bonden |
last post by:
Is there any way to pass parameters to a function and simply know there will
get there without the silly (C like ) complexity of worring about byval and
or perhaps byref ?
(Why bother to have two languages in the first place now ....silliness ; OOP
taken to dysfunctional absurdity ! This idea is too counter productive to
last; MS , your king has no clothes on ...)
|
by: ari |
last post by:
hey all,
i have the following 2 classes:
Public Class DataAccessLayer
....
....
Public Sub GetRecords(ByRef ds As DataSet1)
ds = New DataSet1
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |