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 5 2094
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.borwr ote in message news:%2******** *******@TK2MSFT NGP06.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
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.borwr ote in message
news:%2******** *******@TK2MSFT NGP06.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
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********@rem oveopenmymindre movemetoo.andme netwrote in
message news:eJ******** ******@TK2MSFTN GP04.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.borwr ote in message
news:%2******** *******@TK2MSFT NGP06.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
On Thu, 28 Dec 2006 14:55:57 -0500, "Karl Seguin"
<ka********@rem oveopenmymindre movemetoo.andme netwrote:
>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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 points to something and changes it. The default for
simple data types is ByVal and for objects, it's ByRef.
Am I correct so far? If so, I still don't have a clue as
|
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
variables and arrays are always passed by value and objects (I tried
dictionary ones) are always passed by reference. Is it correct? Or I'm
wrong and I did a mistake on my test code (that you can find below)?
Can someone explain to me why there...
|
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
|
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 changing
the original variable passed)
|
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 allows me to store a
reference to the object I passed by val, to change that object and for the
change to be reflected in the callers copy of the reference (confused?).
Well, what is byref for in that case?
I'm coming from a C++ background...
| |
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, as I expected. I
haven't seen this behavior explained precisely in the .net world yet,
so I wanted to check and make sure I've got it right.
I apologize that this is a bit long. I've tried to keep it concise.
There are code fragments here, but...
|
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 have two languages in the first place now ....silliness ; OOP
taken to dysfunctional absurdity ! This idea is too counter productive to
last; MS , your king has no clothes on ...)
|
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 application. The sub loadValueByVal takes the argument by value so after returning to calling method, the object should be unchanged but it is not
Public Class ITest
Private MyName As String
Public TestId As String
Public Sub New()
|
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 Module1
Class A
|
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
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |