472,984 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

ByRef or ByVal

Hei
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
Nov 20 '05 #1
7 5465
They are not the same thing. What do you mean "which one should prefer"? Use
the one that you need to use.

For example:
http://groups.google.com/groups?selm...&output=gplain

(watch for wrapping)
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/
"Hei" <ch******@msn.com> wrote in message
news:eV**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2
"Hei" <ch******@msn.com> schrieb

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)


My personal basic rule: If the procedure shouldn't change the passed value,
declare it ByVal, otherwise ByRef.

The "passed value" is the reference to an object if it is a reference type.
The "passed value" is the object itself if it is a value type.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
* "Hei" <ch******@msn.com> scripsit:
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)


There _is_ a difference between 'ByVal' and 'ByRef'. In VB.NET, 'ByVal'
is used by default because it often makes more sense. 'ByRef' is only
relevant for reference types, if the _reference_ itself should be
changed by the procedure.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb
* "Hei" <ch******@msn.com> scripsit:
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)


There _is_ a difference between 'ByVal' and 'ByRef'. In VB.NET,
'ByVal' is used by default because it often makes more sense.
'ByRef' is only relevant for reference types, if the _reference_
itself should be changed by the procedure.

Why do you think that ByRef is only relevant for reference types??
--
Armin

Nov 20 '05 #5
* "Armin Zingler" <az*******@freenet.de> scripsit:
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)


There _is_ a difference between 'ByVal' and 'ByRef'. In VB.NET,
'ByVal' is used by default because it often makes more sense.
'ByRef' is only relevant for reference types, if the _reference_
itself should be changed by the procedure.


Why do you think that ByRef is only relevant for reference types??


Maybe my bad skills in the English language caused the misinterpretation
on your side. If wanted to point out when 'ByRef' makes sense when used
with reference types.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb
* "Armin Zingler" <az*******@freenet.de> scripsit:
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)

There _is_ a difference between 'ByVal' and 'ByRef'. In
VB.NET, 'ByVal' is used by default because it often makes more
sense. 'ByRef' is only relevant for reference types, if the
_reference_ itself should be changed by the procedure.
Why do you think that ByRef is only relevant for reference
types??


Maybe my bad skills in the English language


haha.. ;)
caused the
misinterpretation on your side. If wanted to point out when 'ByRef'
makes sense when used with reference types.


I still don't understand - but, no need for an explanation. We know what
byval/byref do. ;-)
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #7
Hei,
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) Are you sure you know the difference? ;-)

ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters by default are passed ByVal, you should only pass a parameter
ByRef when you have to, which is when you need to modify the callers
variable.

Less memory use & better performance should not be a factor in choosing
ByVal & ByRef. The only time to consider ByRef for less memory & performance
is when passing large structures (structures as in defined with the
Structure keyword), however structures should never be large!

Structure Usage Guidelines.
http://msdn.microsoft.com/library/de...guidelines.asp

A Reference Type is an object that exists on the heap. If I have a variable
that is a reference type and assign the variable to another variable. Both
variables will be pointing to the same object on the heap.

Dim x As Person
x = New Person()
Dim y As Person
y = x

Both x & y are the exact same Person object on the heap.

A Value Type does not live on the Heap. If I have a value type variable and
I assign it to another variable, a copy of the value is made.

Dim x As Integer
x = 100
Dim y As Integer
y = x

Although both x & y have the value 100, they are physically different values
as a copy was made.

Now when you pass a variable to a ByVal parameter a copy of the variable is
made. So for a Reference Type a copy of the reference is made, which means
there is still only one object on the heap & two references to that object.
For a Value Type a copy of the value is made.

When you pass a variable to a ByRef parameter a reference to that variable
is made. So for a Reference Type you have a reference to a reference to the
object, for a Value Type you have a reference to the value.

Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.

Hope this helps
Jay
"Hei" <ch******@msn.com> wrote in message
news:eV**************@TK2MSFTNGP10.phx.gbl... 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

Nov 20 '05 #8

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...
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
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.