PC Review


Reply
Thread Tools Rate Thread

Best practice ByVal or ByRef

 
 
rob
Guest
Posts: n/a
 
      25th Aug 2003
I have have a xmlTextWriter that calls a number of sub
routines (up to 60), the only paramter past in is the
xmlTextWriter and the paramenter past out is the
xmlTextWriter
Should I pass this paramter byVal or byRef
I know passing byVal is the best praticse but as the
xmlTextWriter gets larger, in my mind it seems like an
inefficient use of resources to create some many copy's of
the same variable
What is the best practise in a case like this
 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      25th Aug 2003
"rob" <(E-Mail Removed)> schrieb
> I have have a xmlTextWriter that calls a number of sub
> routines (up to 60), the only paramter past in is the
> xmlTextWriter and the paramenter past out is the
> xmlTextWriter
> Should I pass this paramter byVal or byRef
> I know passing byVal is the best praticse but as the
> xmlTextWriter gets larger, in my mind it seems like an
> inefficient use of resources to create some many copy's of
> the same variable
> What is the best practise in a case like this


As an xmlTextWriter is probably a reference type, the size of the object
doesn't matter. It's always only the reference (4 bytes) passed.


--
Armin

 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      25th Aug 2003
Hello,

"rob" <(E-Mail Removed)> schrieb:
> I have have a xmlTextWriter that calls a number of sub
> routines (up to 60), the only paramter past in is the
> xmlTextWriter and the paramenter past out is the
> xmlTextWriter
> Should I pass this paramter byVal or byRef
> I know passing byVal is the best praticse but as the
> xmlTextWriter gets larger, in my mind it seems like an
> inefficient use of resources to create some many copy's of
> the same variable


Have a look at the docs for the difference between ByVal and ByRef for
reference types. Passing the XmlTextWriter ByVal will not create a copy of
the XmlTextWriter.

Regards,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet


 
Reply With Quote
 
David Browne
Guest
Posts: n/a
 
      25th Aug 2003

"rob" <(E-Mail Removed)> wrote in message
news:0d3901c36b4f$e7dacb10$(E-Mail Removed)...
> I have have a xmlTextWriter that calls a number of sub
> routines (up to 60), the only paramter past in is the
> xmlTextWriter and the paramenter past out is the
> xmlTextWriter
> Should I pass this paramter byVal or byRef
> I know passing byVal is the best praticse but as the
> xmlTextWriter gets larger, in my mind it seems like an
> inefficient use of resources to create some many copy's of
> the same variable
> What is the best practise in a case like this


ByVal.

XmlTextWriter is a reference type. You can tell this because it does not
inherit System.ValueType. For reference types pass them ByVal 99.999999% of
the time.

When you pass a reference type ByVal the only thing that actually gets
copied is the 32 bit heap address of the referenced object. Only pass
reference types ByRef if you need to change which object is referenced.

When you pass a reference type ByRef what gets copied is the stack address
of the pointer that holds the heap address of the refrenced object. The
code in the invoked procedure can then edit the value of this pointer making
it point to a different heap address. This can be highly confusing and
unexpected, so in addition to being no more efficient, passing reference
types ByRef is bad practice unless you intend to use this behavior.



David


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ByRef vs ByVal =?Utf-8?B?R3Jk?= Microsoft Excel Programming 2 11th May 2007 08:23 PM
ByVal vs. ByRef Chris Microsoft Dot NET Framework 4 22nd Jul 2005 09:14 PM
ByVal and ByRef in VB.Net =?Utf-8?B?TWljaGVs?= Microsoft Dot NET 3 1st Mar 2005 08:23 PM
Byval vs. byref Iams Microsoft ASP .NET 5 24th Feb 2005 04:48 PM
ByVal and ByRef Goncalo Microsoft ASP .NET 1 4th Dec 2003 09:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:39 AM.