Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
ByVal vs ByRef when generating tree
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
teddybear  
View profile  
 More options Nov 15 2009, 5:39 pm
Newsgroups: microsoft.public.scripting.vbscript
From: teddybear <te...@yahoo.com>
Date: Sun, 15 Nov 2009 09:39:55 -0800 (PST)
Local: Sun, Nov 15 2009 5:39 pm
Subject: ByVal vs ByRef when generating tree
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.

    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ekkehard.horner  
View profile  
 More options Nov 15 2009, 6:58 pm
Newsgroups: microsoft.public.scripting.vbscript
From: "ekkehard.horner" <ekkehard.hor...@arcor.de>
Date: Sun, 15 Nov 2009 19:58:41 +0100
Local: Sun, Nov 15 2009 6:58 pm
Subject: Re: ByVal vs ByRef when generating tree
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.

In VBScript an object is *not* a contiguous area of memory holding
data, but a pointer/reference/handle giving indirect access to such data.
Copying an object (whether by assignment or by passing by value) is cheap,
but seldom (never ?) useful.

See:

   Dim sXML : sXML     = "<root><child>one</child></root>"
   Dim oXML : Set oXML = CreateObject( "MSXML2.DOMDocument" )
   oXML.loadXML sXML
   WScript.Echo 0, oXML.xml
   Dim ndChild : Set ndChild = oXML.documentElement.firstChild
   byRefXml ndChild
   WScript.Echo 1, ndChild.xml
   byValXml ndChild
   WScript.Echo 2, ndChild.xml
   byValValXml (ndChild)
   WScript.Echo 3, ndChild.xml

output:

=== XmlByRefVal: ByRef/ByVal for xml no
0 <root><child>one</child></root>

1 <child>two</child>
2 <child>three</child>
3 <child>four</child>
=== XmlByRefVal: 0 done (00:00:00) ====


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christoph Basedau  
View profile  
 More options Nov 16 2009, 10:30 pm
Newsgroups: microsoft.public.scripting.vbscript
From: Christoph Basedau <cbase...@hotmail.de>
Date: Mon, 16 Nov 2009 23:30:44 +0100
Local: Mon, Nov 16 2009 10:30 pm
Subject: Re: ByVal vs ByRef when generating tree
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


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google