Published: Wednesday, May 02, 2001
Object References in VBScript (Variables and Values in VBScript, Part 3), Part 2
By Bill Wilkinson
Read Part 1
In Part 1 we looked at what, specifically, object references were and
discussed their limitations on copying objects within VBScript. In this part we'll examine how VBScript handles
copying arrays.
What About Arrays?
So we can't copy objects. Can we copy arrays? The answer may surprise you. Again, a small demonstration seems
in order:
<HTML><BODY>
<%
' We will see what happens here ...
Sub alterArrayByVal( ByVal ar )
ar( UBound(ar) ) = "CHANGED in alterArrayByVal!"
End Sub
' ... and here ...
Sub alterArrayByRef( ByRef ar )
ar( UBound(ar) ) = "CHANGED in alterArrayByRef!"
End Sub
' our initial array...
ar1 = Array("this","array","is","unchanged")
' is assigned to a second variable...
ar2 = ar1
Response.Write "After copying ar1 to ar2:<UL>" _
& "<LI>ar1 = " & Join( ar1 ) _
& "<LI>ar2 = " & Join( ar2 ) & "</UL>" & vbNewLine
' Now try various alterations of the arrays:
'
ar2(1) = "second array"
Response.Write "After changing element ar2(1):<UL>" _
& "<LI>ar1 = " & Join( ar1 ) _
& "<LI>ar2 = " & Join( ar2 ) & "</UL>" & vbNewLine
alterArrayByVal ar1
Response.Write "After alter by VAL of ar1:<UL>" _
& "<LI>ar1 = " & Join( ar1 ) _
& "<LI>ar2 = " & Join( ar2 ) & "</UL>" & vbNewLine
alterArrayByRef ar1
Response.Write "After alter by REF of ar1:<UL>" _
& "<LI>ar1 = " & Join( ar1 ) _
& "<LI>ar2 = " & Join( ar2 ) & "</UL>" & vbNewLine
alterArrayByRef ar2
Response.Write "After alter by REF of ar2:<UL>" _
& "<LI>ar1 = " & Join( ar1 ) _
& "<LI>ar2 = " & Join( ar2 ) & "</UL>" & vbNewLine
%>
</BODY></HTML>
|
You really should copy that code to your own ASP page and try it out. And then try as many variations on it as
you can think of! But since this is an article, we'll have to show you the output:
After copying ar1 to ar2:
· ar1 = this array is unchanged
· ar2 = this array is unchanged
After changing ar2(1):
· ar1 = this array is unchanged
· ar2 = this second array is unchanged
After alter by VAL of ar1:
· ar1 = this array is unchanged
· ar2 = this second array is unchanged
After alter by REF of ar1:
· ar1 = this array is CHANGED in alterArrayByRef!
· ar2 = this second array is unchanged
After alter by REF of ar2:
· ar1 = this array is CHANGED in alterArrayByRef!
· ar2 = this second array is CHANGED in alterArrayByRef!
Did you look very carefully at those results? Indeed, after the assignment of ar1 to ar2,
the two arrays are the same. But then look: we change one of the elements of one of the arrays ... and the other
array remains unchanged! We don't need to go any further: Arrays are always copied! You can not have two
variables refer to the same array in VBScript! It just does not happen.
What About ByRef?
"But wait!" you exclaim, "I can see that when you call the alterArrayByRef subroutine that changes to
the array inside the Sub are reflected in the array outside the Sub."
Yes. True. And, if you want to think of it this way, this is the exception that proves the rule. But a better
way of looking at it, I think, is to say that anytime you are inside a Sub or Function
and are using a ByRef parameter, you don't really have another variable ... you just have an "alias"
for the caller's variable. Without going into too many details, the way VBScript is written this is almost exactly
what is happening. It is interesting to note that Java only allows calling ByVal (but then, it never
copies arrays!) and many C++ programmers have a hard time understanding when a call is truly ByRef
in that language.
Sometimes, one might hope for the seeming simplicity of Java, but trust me that the programmer can pay a price for
that simplicity. The lesson, if there is one? Be happy with what you have! And use ByRef for most
object and array references and ByVal for most primitive values passed to subroutines and functions.
A Last Word
Since we've gotten into a discussion of ByRef and ByVal, it is worth dragging up this
old article that warns of the dangers of putting parentheses into your
VBScript code when you don't need to!
Where could we go next with this topic area? Really, I don't think there is a lot left, except how the
capabilities and limitations of VBScript (and JavaScript) mesh with those of Visual Basic and C++, especially as
the pertain to writing ActiveX components for use in your scripting. But that's been covered in so many articles
and forum posts that I think I'll leave it for now.
Happy Programming!
By Bill Wilkinson