teddybear schrieb:
> I have a tree data structure in xml. I currently use ByVal and
> recursion to draw the tree. My understanding is that each time I pass
> the child node to the recursive function, it creates a copy of the
> node containing items at the point and all subitems. I'm wondering it
> it would make a lot of difference in memory usage if I pass the child
> node by ref.
Afacis you cannot truly pass an object by value in VBScript.
Even if you denote 'ByVal' to the formal parameter:
Function PassByVal (ByVal objectArg)
VBS will effectively still pass an reference to the original object.
All that's passed by value is the long variable, that holds the address
of the object. But there'll be no new instancing or even
cloning of member data in any way.
If you modify some of the objects data inside the body of the supposed
ByVal-function, the object data remains modified after having returned,
which clearly indicates that you worked on the same instance.
This makes a difference to languages that support true by-value-passing
- internally creating a shallow copy (memcopy) - as does C++.
See the listing below, that shows that ByVal or ByRef makes no
difference if the parameter is an object.
You get the same effect if you use test the mechanism on
ActiveX-objects instantiated by CreateObject
rather then VBScript class-objects.
If you need copies, you have to copy the data on your own - unless
your object already supports some kind of clone mechanism.
Christoph
set obj = new ByValTest
obj.Value = 10
wsh.echo "Outside any func:", obj.Value '10
ByValPassing obj
wsh.echo "Outside any func:", obj.Value 'now 11,
'shold remain 10 if byval would create a cpy
ByRefPassing obj
wsh.echo "Outside any func:", obj.Value 'now 12
class ByValTest
private valueData
public property get Value : Value = valueData : end property
public property let Value(dat) : valueData = dat : end property
end class
sub ByValPassing(ByVal objArg)
objArg.Value = objArg.Value + 1
wsh.echo "Inside byval-func:", objArg.Value
end sub
sub ByRefPassing(ByRef objArg)
objArg.Value = objArg.Value + 1
wsh.echo "Inside byref-func:", objArg.Value
end sub