473,626 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

byVal Vs. byRef

Hello,

Ok here is the senerio:

.....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Coun t > 0 'This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

'Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh
Nov 20 '05 #1
19 2407
Rob:

I'm not sure what you are asking, but an ArrayList is a Reference type, so
even if you pass it by value, all you are passing is a Copy of the REFERENCE
to the object. For all intents and purposes you won't notice the
difference. Yes, if you pass this reference type ByVal and the function
it's passed to makes any changes to it, the original ArrayList will be
changed. So the only difference between passing a Ref type byval or byRef
is that in the first case you pass a copy of the Reference, in the second
you pass the actual reference. If you want a unique copy that you can do
with what you please, you'll probalby need to opt for a deep clone of it.

HTH,

Bill
"Rob Panosh" <ro************ ************@as dsoftadfdware.c om> wrote in
message news:Om******** *****@TK2MSFTNG P11.phx.gbl...
Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Coun t > 0 'This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

'Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh

Nov 20 '05 #2
that should be a byref if you are going to make changes to it that will be
visible outside of that sub... byval only makes a copy of the data.. byref
makes a pointer to the data, which is what you need if you are going to make
changes that are visible outside of that test sub, or they will just get
thrown out as the sub exits... and why do you have return in there?
"Rob Panosh" <ro************ ************@as dsoftadfdware.c om> wrote in
message news:Om******** *****@TK2MSFTNG P11.phx.gbl...
Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Coun t > 0 'This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

'Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh

Nov 20 '05 #3
William,
even if you pass it by value, all you are passing is a Copy of the REFERENCE to the object. That is what I was asking. I could see a difference between them. So are
there any peformance implications here?
My guess byRef would have better performance because it doesn't have to make
a copied of the reference.

Thanks for your help and timely reponse

Rob
"William Ryan" <do********@com cast.nospam.net > wrote in message
news:uS******** ******@TK2MSFTN GP09.phx.gbl... Rob:

I'm not sure what you are asking, but an ArrayList is a Reference type, so
even if you pass it by value, all you are passing is a Copy of the REFERENCE to the object. For all intents and purposes you won't notice the
difference. Yes, if you pass this reference type ByVal and the function
it's passed to makes any changes to it, the original ArrayList will be
changed. So the only difference between passing a Ref type byval or byRef
is that in the first case you pass a copy of the Reference, in the second
you pass the actual reference. If you want a unique copy that you can do
with what you please, you'll probalby need to opt for a deep clone of it.

HTH,

Bill
"Rob Panosh" <ro************ ************@as dsoftadfdware.c om> wrote in
message news:Om******** *****@TK2MSFTNG P11.phx.gbl...
Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Coun t > 0 'This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

'Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh


Nov 20 '05 #4
> that should be a byref if you are going to make changes to it that will be
It doesn't make any difference how I pass it I can still see the changes.
thrown out as the sub exits... and why do you have return in there? fat fingers .... didn't mean to put it there.

rob

"Brian Henry" <brianiup[nospam]@adelphia.net> wrote in message
news:e4******** ******@TK2MSFTN GP11.phx.gbl... that should be a byref if you are going to make changes to it that will be
visible outside of that sub... byval only makes a copy of the data.. byref
makes a pointer to the data, which is what you need if you are going to make changes that are visible outside of that test sub, or they will just get
thrown out as the sub exits... and why do you have return in there?
"Rob Panosh" <ro************ ************@as dsoftadfdware.c om> wrote in
message news:Om******** *****@TK2MSFTNG P11.phx.gbl...
Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Coun t > 0 'This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

'Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh


Nov 20 '05 #5
Rob,

If Test_A was function and you were assigning an array list back to
myArrayList then ByVal is OK. If it's a sub and you want the original
object modified then pass it as ByRef. In this instance, Test_A only
creates a new ArrayList object (X), adds some items and then does nothing,
myArrayList isn't modified at all.

Hope this helps

Glen

"Rob Panosh" <ro************ ************@as dsoftadfdware.c om> wrote in
message news:Om******** *****@TK2MSFTNG P11.phx.gbl...
Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Coun t > 0 'This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

'Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh

Nov 20 '05 #6
William,
even if you pass it by value, all you are passing is a Copy of the REFERENCE to the object. That is what I was asking. I could see a difference between them. So are
there any peformance implications here?
My guess byRef would have better performance because it doesn't have to make
a copied of the reference.

Thanks for your help and timely reponse

Rob
"William Ryan" <do********@com cast.nospam.net > wrote in message
news:uS******** ******@TK2MSFTN GP09.phx.gbl... Rob:

I'm not sure what you are asking, but an ArrayList is a Reference type, so
even if you pass it by value, all you are passing is a Copy of the REFERENCE to the object. For all intents and purposes you won't notice the
difference. Yes, if you pass this reference type ByVal and the function
it's passed to makes any changes to it, the original ArrayList will be
changed. So the only difference between passing a Ref type byval or byRef
is that in the first case you pass a copy of the Reference, in the second
you pass the actual reference. If you want a unique copy that you can do
with what you please, you'll probalby need to opt for a deep clone of it.

HTH,

Bill
"Rob Panosh" <ro************ ************@as dsoftadfdware.c om> wrote in
message news:Om******** *****@TK2MSFTNG P11.phx.gbl...
Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Coun t > 0 'This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

'Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh


Nov 20 '05 #7
> that should be a byref if you are going to make changes to it that will be
It doesn't make any difference how I pass it I can still see the changes.
thrown out as the sub exits... and why do you have return in there? fat fingers .... didn't mean to put it there.

rob

"Brian Henry" <brianiup[nospam]@adelphia.net> wrote in message
news:e4******** ******@TK2MSFTN GP11.phx.gbl... that should be a byref if you are going to make changes to it that will be
visible outside of that sub... byval only makes a copy of the data.. byref
makes a pointer to the data, which is what you need if you are going to make changes that are visible outside of that test sub, or they will just get
thrown out as the sub exits... and why do you have return in there?
"Rob Panosh" <ro************ ************@as dsoftadfdware.c om> wrote in
message news:Om******** *****@TK2MSFTNG P11.phx.gbl...
Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Coun t > 0 'This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

'Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh


Nov 20 '05 #8
Rob,

If Test_A was function and you were assigning an array list back to
myArrayList then ByVal is OK. If it's a sub and you want the original
object modified then pass it as ByRef. In this instance, Test_A only
creates a new ArrayList object (X), adds some items and then does nothing,
myArrayList isn't modified at all.

Hope this helps

Glen

"Rob Panosh" <ro************ ************@as dsoftadfdware.c om> wrote in
message news:Om******** *****@TK2MSFTNG P11.phx.gbl...
Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Coun t > 0 'This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

'Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh

Nov 20 '05 #9
Rob,
In addition to the others comments:
When should I make the incoming parameter X as byRef? Short answer: Only when you need to modify the caller's variable!

Long answer:
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

"Rob Panosh" <ro************ ************@as dsoftadfdware.c om> wrote in
message news:Om******** *****@TK2MSFTNG P11.phx.gbl... Hello,

Ok here is the senerio:

....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Coun t > 0 'This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

'Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN
When should I make the incoming parameter X as byRef?
Thanks,
Rob Panosh

Nov 20 '05 #10

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

Similar topics

8
383
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
7
5497
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
12307
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)
14
2499
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...
14
1526
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed something? Regards /Niklas Public Class Main Shared Sub Main() Dim testPropObj As New MyPropertObject testPropObj.MyInt = 1
4
2465
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...
7
1879
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 ...)
2
10264
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()
8
2209
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
0
8272
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8205
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8713
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8514
tracyyun
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...
1
6126
isladogs
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...
0
5579
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();...
0
4094
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...
1
2632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.