WebRef.eu  - Internet Marketing and Online Business Resources  

Home / Site Map | Contact

 

ByRef vs ByVal in ASP Classic

In ASP Classic, there are two ways that a variable can be passed to a Sub Routine (or Function), ByRef or ByVal. The differences between them are shown in the table below:


ByRef ByVal
By Reference By Value
A reference to the actual data in memory A copy of the actual data
Faster because it references the actual data at a given memory location Slower because it involves copying the actual data
Passing a variable to a Sub Routine ByRef means that the data stored in the variable can be permanently changed. So, if you want the change made to a variable in a Sub Routine to persist, use ByRef. A new instance of the variable is created and given to the Sub Routine. Any changes made to the value of this variable in the Sub Routine
have no effect on the value of the original variable passed in.
Can permanently alter the data stored in the variable that was passed in. Cannot permanently alter the data stored in the variable that was passed in.
ByRef is the actual data, and can affect the actual data. ByVal is a copy of the actual data, and can't affect the actual data.
The default in VB Script The default in ASP.net


Demo Script:

<%
Sub AddToValues(ByRef iSnakes, ByVal iRabbits, ByRef iOtters)

iSnakes = iSnakes + 1
iRabbits = iRabbits + 1

iOtters = iRabbits

End Sub


Dim iSnakes, iRabbits
'assign initial values
iSnakes = 1
iRabbits = 1

Dim iOtters
iOtters = 0

'call the Sub
AddToValues iSnakes, iRabbits, iOtters
%>


< !--display variable values after the call to the Sub-->
iSnakes value is now <%=iSnakes%><br>
iRabbits value is now <%=iRabbits%><br>
iOtters value is now <%=iOtters%><br>
<br>

Explanation:<br>
iSnakes starts off with a value of 1, gets passed into the Sub Routine ByRef, meaning the actual data is being used, gets incremented in the Sub Routine by 1, giving 2. As the actual data is being used, the value of iSnakes is permanently changed to 2, so this is the value we see when we display iSnakes later.<br><br>

iRabbits starts off with a value of 1, gets passed into the Sub Routine ByVal, meaning a copy of the data is created and used by the Sub Routine, is incremented by 1 in the Sub Routine giving 2 in the Sub Routine (as proven later by iOtters), but this only applies in the Sub Routine and the actual data is not affected, so when iRabbits is displayed later, its value is still 1.<br><br>

iOtters just proves that the value of iRabbits did indeed reach 2 whilst it was in the Sub Routine. iOtters has an initial value of 0, it's passed into the Sub Routine ByRef so we are dealing with the actual data for iOtters and it can be permanently changed. In the Sub Routine first we have incremented iRabbits giving 2, then we set iOtters to iRabbits, so iOtters has a value of 2, and this changes the actual iOtters data permanently, so we see a value of 2 for iOtters when it is displayed later.

Run the Demo Script (in a new window)

 




Low Prices UK Shopping

Compare Prices
at LowPrices.co.uk


Home / Site Map | Contact

All Content ©2020 WebRef.eu