473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Byref / Byval?

Hi all,

Until just recently most of my functions would have all been using Byval,
when I realised the advantages of ByRef, where appropriate I have used it
instead, my question - are there any performance reasons for using ByRef
rather than ByVal?

In my (tiny) mind, I see ByRef as passing a reference to an already created
object, I can then use values of that object or do things to it, where-as
ByVal creates a separate instance of it (am I correct?) - if this is the
case, presumably I now have 2 instances of this object which need to be
destroyed - so presumably, not having to destroy two things is better?

Any info appreciated..

Regards

Rob
Dec 28 '06 #1
5 2083
I don't think performance is a criteria for usage, the criteria is purpose. if you want to modify the value of the object or simply
use the value of the object....
"Rob Meade" <ku***************@edaem.borwrote in message news:%2***************@TK2MSFTNGP06.phx.gbl...
Hi all,

Until just recently most of my functions would have all been using Byval, when I realised the advantages of ByRef, where
appropriate I have used it instead, my question - are there any performance reasons for using ByRef rather than ByVal?

In my (tiny) mind, I see ByRef as passing a reference to an already created object, I can then use values of that object or do
things to it, where-as ByVal creates a separate instance of it (am I correct?) - if this is the case, presumably I now have 2
instances of this object which need to be destroyed - so presumably, not having to destroy two things is better?

Any info appreciated..

Regards

Rob

Dec 28 '06 #2
There are two types of values in .NET : Value types and Reference types.

Value types are all the core primitives, including ints, chars, bytes and
any custom structure you create.

Reference types are everything else, including string, exceptions, web/win
controls, ..., ... , ... or any custom objects you use

For value types, ByRef and ByVal probably have the impact you think it
should - if you pass it byref and change the value, it won't change in the
calling method..

For reference types the difference is extremely subtle. If you understand
that any variable that holds a reference type is actually a pointer
then...ByVal passes a copy of the pointer, so if you change any properties
or call a method on the object, it WILL actually modify the object (which
probably ISN'T what you would expect, since it's byval). ByRef passes the
actual pointer, so not only does it behave exactly like ByVal, but if you
chance the point location (via assignment), it too will change the object...

Karl

--
http://www.openmymind.net/
http://www.codebetter.com/
"Rob Meade" <ku***************@edaem.borwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
Hi all,

Until just recently most of my functions would have all been using Byval,
when I realised the advantages of ByRef, where appropriate I have used it
instead, my question - are there any performance reasons for using ByRef
rather than ByVal?

In my (tiny) mind, I see ByRef as passing a reference to an already
created object, I can then use values of that object or do things to it,
where-as ByVal creates a separate instance of it (am I correct?) - if this
is the case, presumably I now have 2 instances of this object which need
to be destroyed - so presumably, not having to destroy two things is
better?

Any info appreciated..

Regards

Rob
Dec 28 '06 #3
AND . . .

ByVal on an object will place the object on the stack rather than on the
heap, this has some performance implications, but its not likely you'll
notice it.
"Karl Seguin" <ka********@removeopenmymindremovemetoo.andmenetwr ote in
message news:eJ**************@TK2MSFTNGP04.phx.gbl...
There are two types of values in .NET : Value types and Reference types.

Value types are all the core primitives, including ints, chars, bytes and
any custom structure you create.

Reference types are everything else, including string, exceptions, web/win
controls, ..., ... , ... or any custom objects you use

For value types, ByRef and ByVal probably have the impact you think it
should - if you pass it byref and change the value, it won't change in the
calling method..

For reference types the difference is extremely subtle. If you understand
that any variable that holds a reference type is actually a pointer
then...ByVal passes a copy of the pointer, so if you change any properties
or call a method on the object, it WILL actually modify the object (which
probably ISN'T what you would expect, since it's byval). ByRef passes the
actual pointer, so not only does it behave exactly like ByVal, but if you
chance the point location (via assignment), it too will change the
object...

Karl

--
http://www.openmymind.net/
http://www.codebetter.com/
"Rob Meade" <ku***************@edaem.borwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>Hi all,

Until just recently most of my functions would have all been using Byval,
when I realised the advantages of ByRef, where appropriate I have used it
instead, my question - are there any performance reasons for using ByRef
rather than ByVal?

In my (tiny) mind, I see ByRef as passing a reference to an already
created object, I can then use values of that object or do things to it,
where-as ByVal creates a separate instance of it (am I correct?) - if
this is the case, presumably I now have 2 instances of this object which
need to be destroyed - so presumably, not having to destroy two things is
better?

Any info appreciated..

Regards

Rob

Dec 28 '06 #4
dgk
On Thu, 28 Dec 2006 14:55:57 -0500, "Karl Seguin"
<ka********@removeopenmymindremovemetoo.andmenetwr ote:
>There are two types of values in .NET : Value types and Reference types.

Value types are all the core primitives, including ints, chars, bytes and
any custom structure you create.

Reference types are everything else, including string, exceptions, web/win
controls, ..., ... , ... or any custom objects you use

For value types, ByRef and ByVal probably have the impact you think it
should - if you pass it byref and change the value, it won't change in the
calling method..

For reference types the difference is extremely subtle. If you understand
that any variable that holds a reference type is actually a pointer
then...ByVal passes a copy of the pointer, so if you change any properties
or call a method on the object, it WILL actually modify the object (which
probably ISN'T what you would expect, since it's byval). ByRef passes the
actual pointer, so not only does it behave exactly like ByVal, but if you
chance the point location (via assignment), it too will change the object...

Karl
The performance implication of what Karl just said is that you aren't
creating another instance of an object when you pass it by reference.
You're only creating another pointer to it. So the performance hit is
much less than you might think. If you change a property of an object
that was passed by ref, perhaps color, to RED, then that property is
RED when the caller returns the object.

The idea of actually creating another instance of the object is
problematic. If the object contains other objects, does your
duplicated object contain the original subobjects or does the copy
clone those also. I think that's the difference between a shallow copy
and a deep copy.
Dec 28 '06 #5
wow! There's some reading!

hehe...thanks for all of the replies, you've enlightened me and given me
some more things to investigate..

Cheers

Rob
Dec 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sandy | last post by:
Hello! Help!!!! I have ten zillion books that attempt to describe the difference between ByVal and ByRef and none of them are clear to me. I have gathered that ByVal makes a copy and ByRef...
10
by: Logico | last post by:
Hi everybody, I've tried to use the byref keyword for passing arguments to subroutines and functions in my ASP pages with VBScript, but it seems that both byref and byval are irrilevant, as simple...
7
by: Hei | last post by:
Hi, i know the difference of ByRef and ByVal, in case if use byref or byval don't affect the result which one should prefer? (less memory use, better performance ....issue) thx
4
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
7
by: barrett bonden | last post by:
Is there any way to pass parameters to a function and simply know there will get there without the silly (C like ) complexity of worring about byval and or perhaps byref ? (Why bother to...
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
8
by: Boni | last post by:
Dear all, I found out that I don' understand byVal/byRef in VB. There is a simple example: Why in the first test the result is 10,10 where in the second 0,20. Thanks for your help. Boni Module...
6
by: ari | last post by:
hey all, i have the following 2 classes: Public Class DataAccessLayer .... .... Public Sub GetRecords(ByRef ds As DataSet1) ds = New DataSet1
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.