[thelist] byref vs. byval clarification (was [Off-topic] --ta boo!)

Scott Dexter sgd at ti3.com
Fri Mar 16 17:50:07 CST 2001


> Can you tell sgd uses frontpage? He didn't close his tip tag...

oops, sorry kids

> 
> Ok, here's a question:
> 
> function Yo(aNumber)
>     anotherNumber = aNumber
>     anotherNumber = 14
>     Yo = true
> end function
> 
> x = 1
> good = Yo(x)
> 
> when this occurs in the function:
> 
>     anotherNumber = aNumber
> 
> is the reference assigned, or the value? In other words, does 
> x=1 or does
> x=14?

Since Matt was too lazy to actually try it, I did. The code:
<%
x=1
y=1

function addme(x1,y1)
temp = x1
temp2 = y1
temp = 14
addme = temp+temp2
end function
Response.Write "x before:"&x&"<BR>"
total = addme(x,y)
Response.Write "x after:"&x&"<br>"
%>

the results:
x before:1
x after:1

So, the assignment is a copy. But this is with a scalar. I've watched
behavior of ADO Recordsets (and the Request collection and CapRock's
Dictionary object) where the assignment *is* the reference, unless you throw
CStr() around it to make it a copy.

something to be aware of, and unless you want to affect the original
variable, get in the habit of typing "byval" in your procedure parameter
declarations....

sgd




More information about the thelist mailing list